I have an two elements fruits and crates
fruits is an array containing a list of different fruits like:
["apple","orange","mango","pear"]
crates is an array of objects which contains fruits in it like :
[{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}]
I want to create a new array of objects based following conditions: - check if an entity from fruits array is present in any array of crates.
If present then the final object should have the fruit_name property along with another property called tested set to true.
If not present then tested should be false;
considering above scenario the final output should be as follows:
[
{fruit_name: "apple", tested: true},
{fruit_name: "orange", tested: false},
{fruit_name: "mango", tested: false},
{fruit_name: "pear", tested: true},
]
What I have tried is as follows:
fruits.forEach(x => {
crates.filter((y) => {
let temp;
if (x == y.fruit_name) {
temp = {
fruit: x,
tested: true
}
}
else {
temp = {
time: x,
tested: false
}
}
testedCrates.push(temp);
})
})
the problem with this is, it is returning each fruit two times with both values for tested property.
the output i get is as follows:
[
{fruit: "apple", tested: true},
{time: "apple", tested: false},
{time: "orange", tested: false},
{time: "orange", tested: false},
{time: "mango", tested: false},
{time: "mango", tested: false},
{time: "pear", tested: false},
{time: "pear", tested: false}
]
kindly suggest some fix for this. Additionally if there is a better way of doing this with es6 approach would be helpful. thanks in advance.