比较2个对象以在javascript中创建新的对象数组

时间:2017-12-23 09:57:34

标签: javascript arrays typescript ecmascript-6

我有两个元素水果和板条箱

fruits是一个包含不同水果列表的数组,如:

["apple","orange","mango","pear"]

crates是一个对象数组,其中包含水果,如:

[{id:1,fruit_name: "apple"},{id:2, fruit_name: "pear"}]

我想根据以下条件创建一个新的对象数组: - 检查水果阵列中的实体是否存在于任何一组板条箱中。

如果存在,那么最终对象应该具有fruit_name属性以及另一个名为tested的属性设置为true。     如果不存在,则tested应为false;

考虑到上述情况,最终输出应如下:

[
    {fruit_name: "apple", tested: true},
    {fruit_name: "orange", tested: false},
    {fruit_name: "mango", tested: false},
    {fruit_name: "pear", tested: true},
]

我尝试的内容如下:

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

这个问题是,它使用两个测试属性值返回每个水果两次。

我得到的输出如下:

[
    {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}
]

请提出一些解决方法。 另外,如果使用es6方法有更好的方法,这将有所帮助。 提前谢谢。

6 个答案:

答案 0 :(得分:3)

您可以使用array#maparray#some来检查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);




答案 1 :(得分:1)

创建一个存储在包装箱中的Set个水果,然后map水果阵列到想要的形式:



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




答案 2 :(得分:1)

您可以为crates获取Set并在检查集合时迭代fruits获取新数组。



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




答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

答案 5 :(得分: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);