【问题标题】:Compare 2 objects to create a new array of objects in javascript比较 2 个对象以在 javascript 中创建一个新的对象数组
【发布时间】:2018-06-05 16:43:52
【问题描述】:

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

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 方法有更好的方法会有所帮助。 提前致谢。

【问题讨论】:

    标签: javascript arrays typescript ecmascript-6


    【解决方案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】:

      您可以在检查集合时将Set 用于crates 并迭代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】:

        您可以使用array#map 和array#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);

        【讨论】:

          【解决方案4】:
          fruits.map(fruit => ({
            fruit_name: fruit,
            tested: crates.some(crate => crate.fruit === fruit),
          }));
          

          【讨论】:

            【解决方案5】:
            fruits.map(fruit=>({
                fruit_name: fruit,
                tested: crates.some(x=>x.fruit_name === fruit)
            }))
            

            【讨论】:

            • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
            【解决方案6】:
            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);
            

            【讨论】:

            • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
            猜你喜欢
            • 2019-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-11
            • 2023-02-03
            • 1970-01-01
            • 2021-10-07
            • 1970-01-01
            相关资源
            最近更新 更多