最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter map and return array of ids - Stack Overflow

programmeradmin0浏览0评论

I have this structure :

var entities = {
  1: {'name': 'Fred', 'age': 35},
  2: {'name': 'Hans', 'age': 47},
  3: {'name': 'Bert', 'age': 27}
}

I need something like :

var ids = entities.filter( p => p.age > 30);

which should return an array :

[1, 2]

Is there a convenient way to do this ? e.g. Lodash, etc ?

I have this structure :

var entities = {
  1: {'name': 'Fred', 'age': 35},
  2: {'name': 'Hans', 'age': 47},
  3: {'name': 'Bert', 'age': 27}
}

I need something like :

var ids = entities.filter( p => p.age > 30);

which should return an array :

[1, 2]

Is there a convenient way to do this ? e.g. Lodash, etc ?

Share Improve this question edited Jan 30, 2019 at 15:58 chazsolo 8,5641 gold badge24 silver badges45 bronze badges asked Jan 30, 2019 at 15:53 Wolf359Wolf359 2,7256 gold badges46 silver badges65 bronze badges 2
  • What isn't working for you? – Ryan Wilson Commented Jan 30, 2019 at 15:56
  • @JeremyThille there sort of is. It's called "users". And I'm not trying to be a smartass towards you - this is more to call out the users as being random. I've seen no effort questions or answers get upvotes within minutes of being posted or decent ones getting downvoted within the same time either. I suspect some users of just mass downvoting anything they find with little reason. – VLAZ Commented Jan 31, 2019 at 7:56
Add a ment  | 

6 Answers 6

Reset to default 5

you can use the Object.keys bined with Array.prototype.filter

var entities = {
  1: {'name': 'Fred', 'age': 35},
  2: {'name': 'Hans', 'age': 47},
  3: {'name': 'Bert', 'age': 27}
}
const result = Object.keys(entities).filter(key =>  entities[key].age > 30);
console.log(result);

You can use lodash reduce method. It works for both arrays and objects. Example:

var entities = {
  1: {'name': 'Fred', 'age': 35},
  2: {'name': 'Hans', 'age': 47},
  3: {'name': 'Bert', 'age': 27}
}

var result = _.reduce(entities, (acc, o, key) => {
    if(o.age >= 35) { // add your condition here
       acc.push(key);
     }
    return acc;
}, []);

console.log(result); // ["1", "2"]

filter only works on Array, but there is something interesting: Your object looks like an array!

People that use javascript use to say:

if it walks like a duck and it quacks like a duck, then it is a duck

About this context, something that acts like an array is just an object with numbers as keys plus a length property. And watching your object, you just miss the length property!

Let's add the length property in this way:

entities.length = Math.max(...Object.keys(entities)) + 1; // 4

Then you can transform your array-like object in a real array:

const array = Array.from(entities);

And in the end, you can use all your Array methods on it, also filter:

array
  .filter(p => p.age > 30)
  .map((obj, index) => index);

You could get the entries, filter the age and return the keys (which are obviously strings).

var entities = { 1: {'name': 'Fred', 'age': 35 }, 2: {'name': 'Hans', 'age': 47 }, 3: {'name': 'Bert', 'age': 27 } },
    result = Object
        .entries(entities)
        .filter(({ 1: { age } }) => age > 30)
        .map(([k]) => k);

console.log(result);

You can loop through keys of entities and check for age condition for particular key in entities and filter.

var entities = {
  1: {'name': 'Fred', 'age': 35},
  2: {'name': 'Hans', 'age': 47},
  3: {'name': 'Bert', 'age': 27}
}

let op = Object.keys(entities).filter(e=> entities[e].age > 30)

console.log(op)

Use _.map() and _.keys() to achieve like the code below

var i= -1;
var keys=_.keys(entities);
var person= _.map(entities, function(p) {
    i++;
    if (p.age>30) return keys[i];
});
发布评论

评论列表(0)

  1. 暂无评论