Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在angular 2中合并没有重复项的对象数组_Angular_Typescript - Fatal编程技术网

在angular 2中合并没有重复项的对象数组

在angular 2中合并没有重复项的对象数组,angular,typescript,Angular,Typescript,在Angular2/Typescript(ES6)中,合并两个数组而不重复的最快和最简单的方法是什么 p、 数组包含嵌套对象 我知道已经有很多答案了。这就是我困惑的原因。我试着戴上了,但没法用 提前感谢。这与angular2/typescript无关,我的答案适用于ES6 对源数组中的项使用lodash()中的uniq函数,如下所示: const arrA = [1, 2, 3, 4]; const arrB = [3, 4, 5, 6]; const arr = _.uniq([...arrA

在Angular2/Typescript(ES6)中,合并两个数组而不重复的最快和最简单的方法是什么

p、 数组包含嵌套对象

我知道已经有很多答案了。这就是我困惑的原因。我试着戴上了,但没法用


提前感谢。

这与angular2/typescript无关,我的答案适用于ES6

对源数组中的项使用lodash()中的
uniq
函数,如下所示:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];
对于嵌套对象,使用
uniqBy
()或
uniqWith

const arrA = [{id: 1, n: 'e'}, {id: 2, n: 'z'}];
const arrB = [{id: 2, n: 'z'}, {id: 3, n: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], 'id')
// [{id: 1, n: 'e'}, {id: 2, n: 'z'}, {id: 3, n: 'c'}]
但是您需要一个唯一的标识符(
id
此处)来知道何时复制

[EDIT]如果您没有唯一标识符,并且希望使用整个对象进行重复数据消除,您可以这样做:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];

它将对所有对象进行字符串化,以检查是否存在相同的字符串值,因此请记住,在大型对象/数组上这样做可能代价高昂。拥有唯一标识符是一种更好的做法。

这与angular2/typescript无关,我的答案适用于ES6

对源数组中的项使用lodash()中的
uniq
函数,如下所示:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];
对于嵌套对象,使用
uniqBy
()或
uniqWith

const arrA = [{id: 1, n: 'e'}, {id: 2, n: 'z'}];
const arrB = [{id: 2, n: 'z'}, {id: 3, n: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], 'id')
// [{id: 1, n: 'e'}, {id: 2, n: 'z'}, {id: 3, n: 'c'}]
但是您需要一个唯一的标识符(
id
此处)来知道何时复制

[EDIT]如果您没有唯一标识符,并且希望使用整个对象进行重复数据消除,您可以这样做:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];

它将对所有对象进行字符串化,以检查是否存在相同的字符串值,因此请记住,在大型对象/数组上这样做可能代价高昂。拥有唯一标识符是一种更好的做法。

如果使用typescript,您可以这样做:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];
使用
b.filter(x=>a.every(y=>y!==x))
可以过滤掉重复项。 如果需要将多个数组连接在一起,则需要创建一个函数,在该函数中循环数组,但再次使用与上面相同的方法

function mergeArraysWithoutDuplicates(arrays: any[]){
  var result = [];

  for(const array in arrays){

    result = result.concat(array.filter(x => result.every(y => y !== x));

  }
    return result;
}
代码没有经过测试,但是应该是这样的,这取决于需要。
因此,该函数将返回不带重复项的新数组。

如果使用typescript,可以执行以下操作:

const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
const a = [1, 2, 3];
const b = [2, 3, 4, 5];

result = a.concat(b.filter(x => a.every(y => y !== x))); //[1, 2, 3, 4, 5];
使用
b.filter(x=>a.every(y=>y!==x))
可以过滤掉重复项。 如果需要将多个数组连接在一起,则需要创建一个函数,在该函数中循环数组,但再次使用与上面相同的方法

function mergeArraysWithoutDuplicates(arrays: any[]){
  var result = [];

  for(const array in arrays){

    result = result.concat(array.filter(x => result.every(y => y !== x));

  }
    return result;
}
代码没有经过测试,但是应该是这样的,这取决于需要。
因此,该函数将返回不带重复项的新数组。

我想比较整个对象。在这种情况下uniq可以工作吗?这里的问题是我可以有相同的id,但不同的n。在这种情况下,它不是重复的,而是独特的。我可以有多个比较器(例如id和n)吗?在这种情况下,JSON.stringify示例将完成这项工作。但是,如果您想通过两个属性进行比较,则需要一个自定义方法。或者您可以只运行uniq方法两次或更多次(效率更低)。我想比较整个对象。在这种情况下uniq可以工作吗?这里的问题是我可以有相同的id,但不同的n。在这种情况下,它不是重复的,而是独特的。我可以有多个比较器(例如id和n)吗?在这种情况下,JSON.stringify示例将完成这项工作。但是,如果您想通过两个属性进行比较,则需要一个自定义方法。或者您可以只运行uniq方法两次或两次以上(效率较低)。