In my array, I have data structured like this:
array = [
{ name: "john", tag: ["tag1", "tag2"] },
{ name: "doe", tag: ["tag2"] },
{ name: "jane", tag: ["tag2", "tag3"] }
];
My goal is to create a new array of objects that only contain elements with both "tag2" and "tag3", excluding those with only "tag2" or both "tag1" and "tag2".
The desired result should be:
newArray = [{ name: "jane", tag: ["tag2", "tag3"] }];
To achieve this, I attempted the following method:
tags = ["tag2", "tag3"];
newArray = [];
tags.forEach(tag => {
array.forEach(data => {
data.tag.forEach(item => {
if (item === tag) {
newArray.push(data);
}
});
});
});
However, instead of the expected outcome, I ended up getting all the items in the array.