S
S
sinneren2018-02-05 16:27:42
JavaScript
sinneren, 2018-02-05 16:27:42

How to filter an array of objects by values ​​from an existing object?

Given JSON, in which an array of objects:

[ {
    "name": string,
    "type": string,
    "price": number
},
...
]

And a certain object that contains, albeit, the same (although I would like it without it) keys comparable to objects. But not all, of course, only a part.
I would like to find such objects in JSON from those that are in the value object and display them. That is a normal filter. But I don't understand how to do it. If we use .filter, then we will get an object in value. It turns out that it needs to be filtered again? But the comparison is by value, not by key. Some kind of Object.equal() to come up with? Somehow I got completely confused. Is there an easy solution for this? Only not JQ, it is possible through the Npm package, for example.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Demian Smith, 2018-02-05
@sinneren

There is such a way. Either is called lodash. https://lodash.com
There it will look like this.
Loudesh has an incredible amount of useful features. I wholeheartedly recommend you start using it.

U
uacasszx, 2018-02-05
@uacasszx

let objects = [{a: true, b: 'string1'}, {a: false, b: 'string2'}, {a: false, b: 'string3'}, {a: true, b: 'string4'}, {a: false, b: 'string5'}];
objects.filter(object => (object.a === true && object.b.indexOf('string') !== -1));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question