I have an array of objects. I want to filter to include only objects where all of the elements in the test arrays are present in the original array.
Sample code.
const cards = [
{
id: "1",
name: "J",
tag: ["red", "yellow", "blue", "white"],
size: ["small", "medium"],
},
{
id: "2",
name: "S",
tag: ["red", "green", "black"],
size: ["small", "medium"],
},
{
id: "3",
name: "K",
tag: ["green", "purple", "brown", "white"],
size: ["large"],
}
Test arrays
const sizeArray = ["medium", "small"];
const tagArray = ["red", "black"];
I want the filtered array to only include the second object.
I've tried with filter, includes and no luck looking over many other answers to similar questions.
Thanks a lot.
.filterwith two calls to.everyas insizeArray.every(s => obj.size.includes(s)) && (the same for tagArray)