Javascript - 比较两个数组并将缺少的项添加到第一个数组中

时间:2016-11-24 08:42:10

标签: javascript

假设我有两个数组详细信息1和详细信息2。以下是数组的内容

var details1 =[];
details1[0] = {'ActivityName' : 'Act1',
              'Status' : 'Done'};
details1[1] = {'ActivityName' : 'Act2',
              'Status' : 'InProgress'};
details1[2] = {'ActivityName' : 'Act5',
              'Status' : 'Done'};

var details2 =[];
details2[0] = {'ActivityName' : 'Act2',
              'Status' : 'Done'};
details2[1] = {'ActivityName' : 'Act3',
              'Status' : 'Done'};

我需要比较两个数组并添加缺少的项目,并根据数组详细信息1中的名称更新状态。我的输出应该是

var details1 =[];
details1[0] = {'ActivityName' : 'Act1',
              'Status' : 'Done'};
details1[1] = {'ActivityName' : 'Act2',
              'Status' : 'Done'};
details1[2] = {'ActivityName' : 'Act3',
              'Status' : 'Done'};
details1[3] = {'ActivityName' : 'Act5',
              'Status' : 'Done'};

实现这一目标的最佳方法是什么?

4 个答案:

答案 0 :(得分:1)

将两个数组转换为对象,合并它们,对键进行排序并提取值:



var details1 =[];
details1[0] = {'ActivityName' : 'Act1','Status' : 'Done1'};
details1[1] = {'ActivityName' : 'Act2','Status' : 'InProgress'};
details1[2] = {'ActivityName' : 'Act5','Status' : 'Done1'};

var details2 =[];
details2[0] = {'ActivityName' : 'Act2','Status' : 'Done2'};
details2[1] = {'ActivityName' : 'Act3','Status' : 'Done2'};


let toObject = (a, key) => a.reduce((o, x) =>
    Object.assign(o, {[x[key]]: x}),
    {}
);

let merge = Object.assign(
    toObject(details1, 'ActivityName'),
    toObject(details2, 'ActivityName')
);

let result = Object.keys(merge).sort().map(k => merge[k]);

console.log(result);




答案 1 :(得分:1)

您可以使用哈希表作为项目的参考。



var details1 = [{ ActivityName: 'Act1', Status: 'Done' }, { ActivityName: 'Act2', Status: 'InProgress' }, { ActivityName: 'Act5', Status: 'Done' }],
    details2 = [{ ActivityName: 'Act2', Status: 'Done' }, { ActivityName: 'Act3', Status: 'Done' }],
    hash = Object.create(null);

details1.forEach(function (a) {
    hash[a.ActivityName] = a;
});

details2.forEach(function (a) {
    if (hash[a.ActivityName]) {
        hash[a.ActivityName].Status = a.Status;
        return;
    }
    details1.push(hash[a.ActivityName] = a);
});

details1.sort(function (a, b) { return a.ActivityName.localeCompare(b.ActivityName); });

console.log(details1);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

var details1 = [];
details1[0] = {'ActivityName': 'Act1',
    'Status': 'Done'};
details1[1] = {'ActivityName': 'Act2',
    'Status': 'InProgress'};
details1[2] = {'ActivityName': 'Act5',
    'Status': 'Done'};

var details2 = [];
details2[0] = {'ActivityName': 'Act2',
    'Status': 'Done'};
details2[1] = {'ActivityName': 'Act3',
    'Status': 'Done'};

//Merge function
var __merge = function (arr1, arr2) {
    return arr1.concat(arr2).reduce(function (prev, current, index) {

        if (!(current.ActivityName in prev.keys)) {
            prev.keys[current.ActivityName] = index;
            prev.result.push(current);
        } else {
            prev.result[prev.keys[current.ActivityName]] = current;
        }

        return prev;
    }, {result: [], keys: {}}).result;
}; 

details1 = (__merge(details1, details2));

这就是结果:

[{ ActivityName="Act1",  Status="Done"}, { ActivityName="Act2",  Status="Done"}, { ActivityName="Act5",  Status="Done"}, { ActivityName="Act3",  Status="Done"}]

答案 3 :(得分:0)

使用Map的ES6 +解决方案。

要点是浏览第一个数组并将所有项目添加到地图中,其中ActivityName为关键字,实际对象为值。然后遍历第二个数组,并与现有ActivityName键的值合并或添加新值。

最后,对结果进行排序。

注意:此解决方案不会改变现有数组



const details1 = [{
  'ActivityName': 'Act1',
  'Status': 'Done'
}, {
  'ActivityName': 'Act2',
  'Status': 'InProgress'
}, {
  'ActivityName': 'Act5',
  'Status': 'Done'
}];

const details2 = [{
  'ActivityName': 'Act2',
  'Status': 'Done'
}, {
  'ActivityName': 'Act3',
  'Status': 'Done'
}];

const { assign } = Object;
const map = new Map();

const addToMap = (detail) => {
  const { ActivityName: name } = detail;
  if (map.has(name)) {
    // if detail already exists,
    // create a new object by merging the current detail and the new detail 
    detail = assign({}, map.get(name), detail);
  }
  map.set(name, detail); 
};

// add first then second details to a map
details1.concat(details2).forEach(addToMap);

// sort the keys and map them to values
const result = [...map.keys()]
  .sort()
  .map(key => map.get(key));

// show the new result (NOTE: old arrays are unchanged)
console.log(result);