0

I have an object:

{
   1: {
         name: 'Adam',
         age: 12,
      },
   2: {
         name: 'Michal',
         age: 14,
      },
   3: {
         name: 'Jozef',
         age: 12,
      }
}

I tried using function filter from lodash:

this.knowledges = _.filter(this.knowledges, function (item) {
    if (item.category[1] !== undefined) {
        return arrayOfNumbers.indexOf(item.category[1].category_id) >= 0
    }
})

Variable arrayOfNumbers return array: [1,3] so function filter should return:

{
   1: {
         name: 'Adam',
         age: 12,
      },
   3: {
         name: 'Jozef',
         age: 12,
      }
}

But it returns only first index:

{
   1: {
         name: 'Adam',
         age: 12,
      }
}

How can I fix it ?

3
  • You appear to be overwriting this.knowledges with your filter which is probably a bad idea. Your filter callback also may not return a value Commented Jul 22, 2019 at 7:26
  • 2
    so what does this.knowledges looks like? And what is your object represent for? Commented Jul 22, 2019 at 7:26
  • Hi, this.knowledges my all object (first from my query) Commented Jul 22, 2019 at 7:28

2 Answers 2

3

With lodash you can use _.pick(). Pick takes an object, and an array of properties, and generates a new object with the selected properties:

const knowledges = {1:{name:"Adam",age:12,},2:{name:"Michal",age:14,},3:{name:"Jozef",age:12,}}
const arrayOfNumbers = [1,3]

const result = _.pick(knowledges, arrayOfNumbers)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

The _.pickBy() method works identically to filter for objects, so you can query the objects values, when you decide what to keep.

In this examples, I use _.pickBy() to keep all items with age === 12:

const knowledges = {1:{name:"Adam",age:12,},2:{name:"Michal",age:14,},3:{name:"Jozef",age:12,}}

const result = _.pickBy(knowledges, o => o.age === 12)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, what if I want search by age ?
2

You could use pickBy like this:

const knowledges = {1:{name:"Adam",age:12,},2:{name:"Michal",age:14,},3:{name:"Jozef",age:12,}},
      arrayOfNumbers = [1,3],
      result = _.pickBy(knowledges, (v, k) => arrayOfNumbers.includes(+k));

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

In vanilla JS, you could filter the the entries of the object and create an object using Object.fromEntires() like this:

const knowledges={1:{name:"Adam",age:12,},2:{name:"Michal",age:14,},3:{name:"Jozef",age:12,}},
      arrayOfNumbers=[1,3];

const newObject = Object.fromEntries(
    Object.entries(knowledges).filter(([k, v]) => arrayOfNumbers.includes(+k))
)

console.log(newObject)

2 Comments

..remembering that fromEntries doesn't work on the usual IE and Edge browsers (which are dead, but it's still something you might want to know).
@a.barbieri IE doesn't support const, Object.entries, fromEntries, arrow functions or includes. This was meant to be an vanilla JS alternative. I was adding a lodash answer using pickBy but Ori Drori has provided a better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.