Gautam
Gautam

Reputation: 1056

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.

Upvotes: 2

Views: 88

Answers (6)

Pasha Abe
Pasha Abe

Reputation: 13

fruits.forEach(x => {
    let temp = { fruit: x, tested: false }

    crates.filter(y => {
        if (x === y.fruit_name)
            temp = { fruit: x, tested: true }
})
testedCrates.push(temp);

Upvotes: 0

Vitaliy Andrianov
Vitaliy Andrianov

Reputation: 355

fruits.map(fruit=>({
    fruit_name: fruit,
    tested: crates.some(x=>x.fruit_name === fruit)
}))

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22564

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);

Upvotes: 3

DomA
DomA

Reputation: 138

fruits.map(fruit => ({
  fruit_name: fruit,
  tested: crates.some(crate => crate.fruit === fruit),
}));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

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; }

Upvotes: 1

Ori Drori
Ori Drori

Reputation: 192277

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);

Upvotes: 1

Related Questions