JavaScript Compare 2 objects to create a new array of objects in javascript

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.

Answer:1

You can use array#map and array#some to check if a fruit exist in the crates.

var fruits = ["apple","orange","mango","pear"],
crates = [{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}];

var result = fruits.map(fruit => {
  var tested = crates.some(({fruit_name}) => fruit_name === fruit);
  return {'fruit_name' : fruit, tested};
});
console.log(result);
Answer:2

Create a Set of fruits that exist in the crates, then map the fruits array to the wanted form:

const fruits = ["apple","orange","mango","pear"]
const crates = [{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}];
const cratesSet = new Set(crates.map(({ fruit_name }) => fruit_name));

const result = fruits.map((fruit_name) => ({
  fruit_name,
  tests: cratesSet.has(fruit_name)
}));

console.log(result);
Answer:3

You could take a Set for crates and iterate fruits for a new array while checking the set.

var fruits = ["apple", "orange", "mango", "pear"],
    crates = [{ id: 1, fruit_name: "apple" }, { id: 2, fruit_name: "pear" }],
    cSet = new Set(crates.map(({ fruit_name }) => fruit_name)),
    result = fruits.map(fruit_name => ({ fruit_name, tested: cSet.has(fruit_name) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:4
fruits.map(fruit => ({
  fruit_name: fruit,
  tested: crates.some(crate => crate.fruit === fruit),
}));
Answer:5
fruits.map(fruit=>({
    fruit_name: fruit,
    tested: crates.some(x=>x.fruit_name === fruit)
}))
Answer:6

How can I translate this curl script to an AJAX request in JavaScript? curl -X POST -d "grant_type=password&username=admin&password=Demo1234" -u "<ClientID>:<ClientSecret&...

How can I translate this curl script to an AJAX request in JavaScript? curl -X POST -d "grant_type=password&username=admin&password=Demo1234" -u "<ClientID>:<ClientSecret&...

  1. translate curl to javascript

I'm a bit confused after reading about how to use bitmasks to store boolean values. I would like to have a set of boolean values and then generate a unique integer for each combination of the values. ...

I'm a bit confused after reading about how to use bitmasks to store boolean values. I would like to have a set of boolean values and then generate a unique integer for each combination of the values. ...

I am not able to show two bar charts in one page. I have tried both ways, implementing these in two different <script> sections in and in one <script> section also. If one is shown if I ...

I am not able to show two bar charts in one page. I have tried both ways, implementing these in two different <script> sections in and in one <script> section also. If one is shown if I ...

  1. google charts work offline
  2. google charts not working
  3. google charts not working in ie11
  4. google charts not working in safari
  5. google charts stopped working
  6. google charts not working in chrome
  7. google charts not working in firefox
  8. google charts not working in internet explorer
  9. google charts not working in ie
  10. google charts not working in edge
  11. does google charts work offline
  12. google.charts.setonloadcallback not working
  13. google charts animation not working
  14. google.charts.load not working
  15. google charts colors not working
  16. google charts chartarea not working
  17. google charts options not working
  18. google charts ticks not working
  19. google charts isstacked not working
  20. google charts interpolatenulls not working

Why is the text of the paragraph always one letter behind? Shouldn't it automatically "copy" the value of the input to the paragraph and always be the same text? How can I fix this problem? $(...

Why is the text of the paragraph always one letter behind? Shouldn't it automatically "copy" the value of the input to the paragraph and always be the same text? How can I fix this problem? $(...

  1. input equal output
  2. input equal output energy
  3. should input equal output
  4. does work input equal output
  5. why does input equal output