How do I filter an array object inside of a array of objects Javascript?

Pratik Sawant

I am trying to filter the list by offerings

const questions = [
    { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
    { "id": 1505, "offerings": [{"code": "AA"},{"code": "ABC"}]},
    { "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
];

const filterByArray = ['AB', 'DC'];

My expected outcome is to get back all the array elements based on what's passed in filterByArray

[
    { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
    { "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
]

I tried filtering the array using

var filtered = questions.filter(function(element) {
    return element.offerings.filter(function(cd) {
       return filterByArray.indexOf(cd.code) > -1;
    }).length === filterByArray.length;
});

console.log(filtered)

But this keep returning all array elements.

William Wang

Use Array.some() and Array.includes().

const questions = [
    { "id": 2616, "offerings": [{"code": "AA"},{"code": "AB"}]},
    { "id": 1505, "offerings": [{"code": "AA"},{"code": "ABC"}]},
    { "id": 1500, "offerings": [{"code": "AC"},{"code": "DC"}]}
];

const filterByArray = ['AB', 'DC'];

const output = questions.filter(q => q.offerings.some(o => filterByArray.includes(o.code)));

console.log(output);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mongoose: how to filter an array of objects inside an object

How do I convert array of Objects into one Object in JavaScript?

Fetch request in React: How do I Map through JSON array of object inside of array of objects?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I convert array of objects to object in javascript?

How do i filter an array inside of a array of objects?

How do I filter on an array of objects in Swift?

Filter object by tag (array inside an array of objects)

How do i filter an array inside of a array of objects using filter?

In Javascript, how do I tell array.filter() to duplicate element objects in the new array?

How to filter an array of objects by filtering an array inside of it in javascript

Filter an array inside an object inside an object inside in array of objects

How do I convert array of Objects to one Object in JavaScript?

How do I filter an array of objects with MongoDB Object IDs based on another array of Object IDs?

how to filter an nested objects inside an array in javascript

how do I create an array of objects with array in the object from an object with the same key in javascript

How do I filter and count objects in a javascript array?

Filter an array inside an object inside an array in javascript

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I modify an object value in a cloned array of objects in Javascript

How do i filter an array inside of a array of objects value

How do I filter an array of objects inside of an array of objects Using javascript?

How do I place this object inside an array

How can I filter an array of objects where an array inside the object includes all items from another array?

How to filter an array of object inside an array of objects based on an array and also remove properties of that object?

How do I create an object from array of objects using javascript?

How do i filter the array in object of array?

How to merge array of object inside array of objects in javascript?

How to filter array of objects with array of objects in javascript