2

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.

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

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

Comments

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

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0
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);

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.