比较两个对象,在javascript中创建一个新的对象数组

时间:2021-04-18 22:57:19

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;

如果存在,那么最终的对象应该具有fruit_name属性以及另一个名为testing set to true的属性。如果不存在,那么测试应该是错误的;

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.

请建议一些解决办法。此外,如果有更好的方法来实现es6方法将会很有帮助。提前谢谢。

6 个解决方案

#1


3  

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

您可以使用数组#map和数组#some来检查板条箱中是否存在水果。

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

#2


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

#3


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

#4


1  

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

#5


0  

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

#6


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


3  

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

您可以使用数组#map和数组#some来检查板条箱中是否存在水果。

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

#2


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

#3


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

#4


1  

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

#5


0  

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

#6


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