如何在JS或角度2

时间:2016-12-02 15:35:05

标签: javascript angular

我在角度2 .ts(类型脚本)文件中声明了两个如下所示的数组。

 parentArray:Array<Model> and initialized with  {a,b,c,d}
 modifiedAarray:Array<Model> modified with data {c,e,f,g}

通过比较两个数组如何有效地找到两者之间的差异? [ie]查找新添加的和已存在的元素

2 个答案:

答案 0 :(得分:0)

使用filter

var original = ['a','b','c','d'];
var newarray = ['c','e','f','g'];

var addedEls = newarray.filter(function(a){ return original.indexOf(a) < 0; });
var remvdEls = original.filter(function(a){ return newarray.indexOf(a) < 0; });

alert("These were added: "+addedEls.join(","));
alert("These were removed: "+remvdEls.join(","));

答案 1 :(得分:0)

使用filterObservable

Observable.from(parentArray)
  .filter(item => {
    // Condition here with modifiedAarray
    // IE: return modified.attribute !== item.attribute;
  })
  .subscribe(item => console.log(item));