【问题标题】:Merge two array of object and sort on the value合并两个对象数组并对值进行排序
【发布时间】:2021-04-17 09:31:55
【问题描述】:

const a=[{id:1},{id:5},{id:7},{id:6}]
const b=[{id:7},{id:2},{id:5},{id:9}]

//Merge and sort in single array of object based on unique value

预期输出: [{id:1},{id:2},{id:5},{id:6},{id:7},{id:9}]

尝试先合并再排序无法得到想要的结果 提前致谢

【问题讨论】:

    标签: javascript arrays sorting object merge


    【解决方案1】:

    你可以使用Map Object

    const a = [{ id: 1 }, { id: 5 }, { id: 7 }, { id: 6 }];
    const b = [{ id: 7 }, { id: 2 }, { id: 5 }, { id: 9 }];
    const map = new Map();
    a.forEach((x) => map.set(x.id, { ...x }));
    b.forEach((x) => map.set(x.id, { ...x }));
    const ret = [...map.values()].sort((x, y) => x.id - y.id);
    console.log(ret);

    使用Array.prototype.reduce()方法的另一种解决方案。

    const a = [{ id: 1 }, { id: 5 }, { id: 7 }, { id: 6 }];
    const b = [{ id: 7 }, { id: 2 }, { id: 5 }, { id: 9 }];
    const ret = Object.values(
      [...a, ...b].reduce((prev, c) => {
        const p = prev;
        const key = c.id;
        p[key] = { ...c };
        return p;
      }, {})
    );
    console.log(ret);

    【讨论】:

      【解决方案2】:

      如果 id 仅在 32 位正整数范围内,则可以获取一个对象并获得排序结果。

      const
          a = [{ id: 1 }, { id: 5 }, { id: 7 }, { id: 6 }],
          b = [{ id: 7 }, { id: 2 }, { id: 5 }, { id: 9 }],
          merged = Object.values([...a, ...b].reduce((r, o) => (r[o.id] = o, r), {}));
      
      
      console.log(merged);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

      • 在学习中...你能解释一下reduce里面的作用吗?
      • @Codenewbie,它采用复合 ecxpression 并分配给哈希表并用逗号运算符分隔,它返回对象/哈希表。哈希表是reduces 的累加器,对象键的固有顺序保证了顺序。
      【解决方案3】:

      您可以通过将Array.prototype.reduceArray.prototype.some 组合来获得所需的输出,如下所示:

      const a = [{ id: 1 }, { id: 5 }, { id: 7 }, { id: 6 }];
      const b = [{ id: 7 }, { id: 2 }, { id: 5 }, { id: 9 }];
      
      // Combine the two arrays by using the spread operator
      const c = [...a, ...b].reduce((acc, curr) => {
        // Check if the value is already present in the array
        const isPresentInArray = acc.some((x) => x.id === curr.id);
      
        if (!isPresentInArray) {
          acc.push(curr);
        }
      
        return acc;
      }, []).sort((l, r) => l.id - r.id);
      
      console.log(c);

      【讨论】:

        【解决方案4】:

        使用set(跟踪唯一项目)、filtersort

        const mergeSortUnique = (arrA, arrB) => {
          const set = new Set();
          return [...arrA, ...arrB]
            .filter(({ id }) => ((res = !set.has(id)), set.add(id), res))
            .sort(({ id: a }, { id: b }) => a - b);
        };
        
        const a = [{ id: 1 }, { id: 5 }, { id: 7 }, { id: 6 }];
        const b = [{ id: 7 }, { id: 2 }, { id: 5 }, { id: 9 }];
        
        console.log(mergeSortUnique(a, b))

        【讨论】:

          【解决方案5】:

          我能提供的最短版本是:

          const a=[{id:1},{id:5},{id:7},{id:6}];
          const b=[{id:7},{id:2},{id:5},{id:9}];
          
          const result = [...a, ...b]
          .filter((v,i,k)=>k.findIndex(t=>(t.id === v.id)) === i)
          .sort((x,u)=>x.id - u.id);
          
          console.log(result);

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-07-26
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多