【问题标题】:Compare array objects and show difference比较数组对象并显示差异
【发布时间】:2018-09-27 11:23:12
【问题描述】:

我有两个数组,我想比较并检查其中一个数组中是否有已删除的项目。如果有显示差异(已删除项目)

下面是我想要实现的代码:

 var completedList = [{id:1},{id:2},{id:3},{id:4},{id:7},{id:8}];
    var invalidList = [{id:3},{id:4},{id:5},{id:6}];

    // filter the items from the invalid list, out of the complete list
    var validList = completedList.map((item) => {
        console.log(item.id)
        return item.id;
        //console.log(invalidList.id);
    }).filter(item => {
        Object.keys(invalidList).map(key => {
            console.log(invalidList[key].id)
            //return !invalidList[key].id.includes(item.id);
        });
    })

    console.log(validList); // Print [1,2,7,8]

    // get a Set of the distinct, valid items
    var validItems = new Set(validList);

但这给我带来了很多id's 我如何映射数组并过滤对象属性ID?并且只显示这些数组对象之间的区别。

所以基本上我希望看​​到这些数组之间的差异,所以在这个例子中记录 id 的差异:1,2,5,6,7,8

【问题讨论】:

  • 订单重要吗?请同时添加想要的结果。
  • 所需解决方案的可接受复杂度是多少?
  • @NinaScholz 另一个问题不是重复的。经过一番思考,不是我想要实现的预期输出
  • 你为什么把我的回答当成问题描述?
  • @Sireini 请看我的回答

标签: javascript ecmascript-6 ecmascript-5


【解决方案1】:

您可以通过Set 获得不同。为了获得彼此的差异(对称差异),您需要获得两个差异。

const
    difference = (a, b) => Array.from(b.reduce((s, v) => (s.delete(v), s), new Set(a))),
    getId = ({ id }) => id;

var completedList = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 7 }, { id: 8 }],
    invalidList = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }],
    complete = completedList.map(getId),
    invalid = invalidList.map(getId),
    left = difference(complete, invalid),
    right = difference(invalid, complete),
    result = [...left, ...right]

console.log(result.join(' '));
console.log(left.join(' '));
console.log(right.join(' '));

【讨论】:

    【解决方案2】:

    这应该可以解决问题。

    let completedList = [{id:1},{id:2},{id:3},{id:4},{id:7},{id:8}];
    let invalidList = [{id:3},{id:4},{id:5},{id:6}];
    // filter the items from the invalid list, out of the complete list
    let temp1 = completedList.map(e => e.id);
    let temp2 = invalidList.map(e => e.id);
    let validList = temp1.filter(e => temp2.indexOf(e) === -1);
    // find items only in invalidList
    let difference = temp2.filter(e => temp1.indexOf(e) === -1);
    console.log(validList); // Print [1,2,7,8]
    console.log(difference);
    

    【讨论】:

    • 如果我只想打印 invalidList 的差异
    【解决方案3】:

    我经常依靠 lodash 实现进行比较。 在 lo dash 中,您可以按照以下方式完成工作

    _.intersectionWith(arr1, arr2, _.isEqual) - 相似度 _.differenceWith(arr1, arr2, _.isEqual) - 差异

    这个答案仅限于使用 util 库来完成工作。 如果您正在寻找确切的算法,我肯定会花一些时间来开发它并作为评论回复这篇文章。

    谢谢

    【讨论】:

      【解决方案4】:
      var completedList = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 7 }, { id: 8 }];
      var invalidList = [{ id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }];
      
      //get the items that are in the invalid list but not completed list
      var filteredList1 = invalidList.filter((invalidListItem) => !completedList.find((item) => item.id === invalidListItem.id));
      
      //get the items that are in  the completed list but not in the invalid list
      var filteredList2 = completedList.filter((completedListItem) => !invalidList.find((item) => item.id === completedListItem.id));
      
      //join the two arrays
      var difference = filteredList1.concat(filteredList2);
      
      //display the merged array and sort
      console.log(difference.sort((item1, item2) => { return item1.id > item2.id ? 1 : item1.id < item2.id ? -1 : 0; }));
      
      //outputs 1,2,5,6,7,8
      

      【讨论】:

        猜你喜欢
        • 2011-08-20
        • 2016-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多