Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Javascript 如何通过比较对象中具有不同元素的两个对象数组来筛选数组?_Javascript_Arrays_Lodash_Javascript Objects - Fatal编程技术网

Javascript 如何通过比较对象中具有不同元素的两个对象数组来筛选数组?

Javascript 如何通过比较对象中具有不同元素的两个对象数组来筛选数组?,javascript,arrays,lodash,javascript-objects,Javascript,Arrays,Lodash,Javascript Objects,如何通过比较对象中具有不同元素的两个对象数组来筛选数组? 我有: 我想比较两个数组中的x和y值,并从第一个数组返回not macthing对象,在上面的示例中返回[{x:2,y:1,z:4}] 我试着使用\u0.differenceWith(arr1,arr2,\u0.isEqual)但显然,对于这一点,数组应该具有类似的对象,这与我的情况不同。使用 使用 你很接近正确答案。 lodash中的\uDifferenceWith函数有三个参数:要检查的数组、要排除的值,第三个参数是一个比较器,用于确

如何通过比较对象中具有不同元素的两个对象数组来筛选数组? 我有:

我想比较两个数组中的x和y值,并从第一个数组返回not macthing对象,在上面的示例中返回
[{x:2,y:1,z:4}]
我试着使用
\u0.differenceWith(arr1,arr2,\u0.isEqual)但显然,对于这一点,数组应该具有类似的对象,这与我的情况不同。

使用

使用


你很接近正确答案。 lodash中的
\uDifferenceWith
函数有三个参数:要检查的数组、要排除的值,第三个参数是一个比较器,用于确定需要哪些值。在您的情况下,使用
。.isEqual
是在寻找完全相同的对象(据我所知,这不是您想要的行为)

如果您只关心具有相同的
x
y
值,请尝试使用您的自定义比较器,而不是lodash的
.isEqual
函数

它看起来像这样:

const arr1 = [{ x: 1, y: 2, z:3 }, { x: 2, y: 1, z:4 }];    
const arr2 = [{ x: 1, y: 2, a:5 }, { x: 2, y: 3, a:4 }];

// this is your custom comparator which is called with each value from the two arrays
// I gave descriptive names to the arguments so that it is more clear
const customComparator = (valueFromFirstArray, valueFromSecondArray) =>
  valueFromFirstArray.x === valueFromSecondArray.x
  && valueFromFirstArray.y === valueFromSecondArray.y;

const result = _.differenceWith(arr1, arr2, customComparator);

console.log(result);
// will print [{x: 2, y: 1, z: 4}]
function customComparator(valueFromFirstArray, valueFromSecondArray) {
  return valueFromFirstArray.x === valueFromSecondArray.x
    && valueFromFirstArray.y === valueFromSecondArray.y
}
或者,如果您不熟悉arrow函数,则可以这样声明自定义比较器:

const arr1 = [{ x: 1, y: 2, z:3 }, { x: 2, y: 1, z:4 }];    
const arr2 = [{ x: 1, y: 2, a:5 }, { x: 2, y: 3, a:4 }];

// this is your custom comparator which is called with each value from the two arrays
// I gave descriptive names to the arguments so that it is more clear
const customComparator = (valueFromFirstArray, valueFromSecondArray) =>
  valueFromFirstArray.x === valueFromSecondArray.x
  && valueFromFirstArray.y === valueFromSecondArray.y;

const result = _.differenceWith(arr1, arr2, customComparator);

console.log(result);
// will print [{x: 2, y: 1, z: 4}]
function customComparator(valueFromFirstArray, valueFromSecondArray) {
  return valueFromFirstArray.x === valueFromSecondArray.x
    && valueFromFirstArray.y === valueFromSecondArray.y
}

如果您愿意,您可以在这里与自定义比较器进行交流。

您非常接近正确答案。 lodash中的
\uDifferenceWith
函数有三个参数:要检查的数组、要排除的值,第三个参数是一个比较器,用于确定需要哪些值。在您的情况下,使用
。.isEqual
是在寻找完全相同的对象(据我所知,这不是您想要的行为)

如果您只关心具有相同的
x
y
值,请尝试使用您的自定义比较器,而不是lodash的
.isEqual
函数

它看起来像这样:

const arr1 = [{ x: 1, y: 2, z:3 }, { x: 2, y: 1, z:4 }];    
const arr2 = [{ x: 1, y: 2, a:5 }, { x: 2, y: 3, a:4 }];

// this is your custom comparator which is called with each value from the two arrays
// I gave descriptive names to the arguments so that it is more clear
const customComparator = (valueFromFirstArray, valueFromSecondArray) =>
  valueFromFirstArray.x === valueFromSecondArray.x
  && valueFromFirstArray.y === valueFromSecondArray.y;

const result = _.differenceWith(arr1, arr2, customComparator);

console.log(result);
// will print [{x: 2, y: 1, z: 4}]
function customComparator(valueFromFirstArray, valueFromSecondArray) {
  return valueFromFirstArray.x === valueFromSecondArray.x
    && valueFromFirstArray.y === valueFromSecondArray.y
}
或者,如果您不熟悉arrow函数,则可以这样声明自定义比较器:

const arr1 = [{ x: 1, y: 2, z:3 }, { x: 2, y: 1, z:4 }];    
const arr2 = [{ x: 1, y: 2, a:5 }, { x: 2, y: 3, a:4 }];

// this is your custom comparator which is called with each value from the two arrays
// I gave descriptive names to the arguments so that it is more clear
const customComparator = (valueFromFirstArray, valueFromSecondArray) =>
  valueFromFirstArray.x === valueFromSecondArray.x
  && valueFromFirstArray.y === valueFromSecondArray.y;

const result = _.differenceWith(arr1, arr2, customComparator);

console.log(result);
// will print [{x: 2, y: 1, z: 4}]
function customComparator(valueFromFirstArray, valueFromSecondArray) {
  return valueFromFirstArray.x === valueFromSecondArray.x
    && valueFromFirstArray.y === valueFromSecondArray.y
}

如果您愿意,您可以在这里与自定义比较器进行交互。

我不理解返回
[{x:2,y:1,z:4}]
的数学逻辑。
z:3
不是也不一样吗?@andrewL,我只想返回整个对象,它无法匹配两个元素x和yb,但是返回的数组
x:2
中的第一项在两个数组中都有匹配的元素。x和y都必须匹配。我不理解返回
[{x:2,y:1,z:4}]的数学逻辑
不过。
z:3
也不一样吗?@andrewL,我只想返回整个对象,它无法匹配两个元素x和y,但返回数组
x:2
中的第一项在两个数组中都有匹配的元素。x和y都必须匹配hi Flash,谢谢。但唯一的问题是数组长度不相等,因此这将失败,并且两个数组的数组长度都不是固定的,可以变化。嗨,Flash,谢谢。但唯一的问题是数组长度不相等,因此这将失败,并且两个数组的数组长度都不是固定的,可以变化。