简体   繁体   English

检查一个数组中的 object 是否包含在另一个对象数组中

[英]Check if object from one array is included inside another array of objects

We have 2 arrays我们有 2 个 arrays

const arr1 = [{color: 'red', shape: 'square'}, {color: 'blue', shape: 'circle'}, {color: 'green', shape: 'square}]
const arr2 = [{color: 'red', shape: 'circle'}, {color: 'blue', shape: 'circle'}]

I want to check if at least one item from arr2 is included inside of arr1 (if yes return bool value).我想检查 arr2 中的至少一项是否包含在 arr1 中(如果是,则返回 bool 值)。 So far i tried arr2.some(item => arr1.includes(item)) but this doesn't seem to work.到目前为止,我尝试arr2.some(item => arr1.includes(item))但这似乎不起作用。

you can use this code arr2.some(item => arr1.find(e=>JSON.stringify(e) === JSON.stringify(item))您可以使用此代码 arr2.some(item => arr1.find(e=>JSON.stringify(e) === JSON.stringify(item))

Array.prototype.includes checks if it is equal so it is useful for primitive values. Array.prototype.includes 检查它是否相等,因此它对原始值很有用。 To check if it deep equal (for arrays and objects) you can use find method.要检查它是否深度相等(对于 arrays 和对象),您可以使用 find 方法。 So correct solution would be:所以正确的解决方案是:

arr2.some(item => arr1.find(e=>e.color==item.color&&e.shape==item.shape))

Array includes() matches the object using reference equality, it does not deep compare item object's properties.数组includes()使用引用相等匹配 object,它不深入比较项目对象的属性。 So, you also need an object matcher.因此,您还需要一个 object 匹配器。

const overlap = arr1.some(i1 => arr2.some(i2 => matchObj(i1,i2)));

and you will have to write an implementation of matchObj(obj1, obj2) .你将不得不编写matchObj(obj1, obj2)的实现。

You can also use lodash's isEqual() or intersecttionWith() method:您还可以使用 lodash 的isEqual()intersecttionWith()方法:

 const arr1 = [{color: 'red', shape: 'square'}, {color: 'blue', shape: 'circle'}, {color: 'green', shape: 'square'}]; const arr2 = [{color: 'red', shape: 'circle'}, {color: 'blue', shape: 'circle'}]; // check for overlap const hasCommon = _.some(arr1, i1 => _.some(arr2, i2 => _.isEqual(i1, i2))); console.log('hasCommon:', hasCommon); // get common items const commonItems = _.intersectionWith(arr1, arr2, _.isEqual); console.log('commonItems:', commonItems);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

If your array of object is small, not too big.如果您的 object 数组很小,也不会太大。 You can use JSON.stringify too check.您也可以使用 JSON.stringify 检查。

const arr1_str = JSON.stringify(arr1);
arr2.some(item2 => arr1_str.includes(JSON.stringify(item2)));

 const arr1 = [{color: 'red', shape: 'square'}, {color: 'blue', shape: 'circle'}, {color: 'green', shape: 'square'}]; const arr2 = [{color: 'red', shape: 'circle'}, {color: 'blue', shape: 'circle'}]; const arr1_str = JSON.stringify(arr1); console.log(arr2.some(item2 => arr1_str.includes(JSON.stringify(item2))));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查一个对象数组的字段是否从另一个对象数组中丢失 - Check if field of one array of objects is missing from another array of objects 对象内的数组:从另一个对象的数组有条件地更新一个对象的数组 - Arrays Inside Objects: Conditional Updating Of One Object's Array From Another Object's Array 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 如何检查数组的所有对象是否包含在另一个数组中? - How to check if all objects of array are included another array? javascript通过另一个对象数组内的属性检查添加或替换对象 - javascript add or replace an object by property check inside another array of objects 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript? 在 React 中将对象从一个对象数组复制到另一个对象 - Copy object from one array of objects to another in React 将 object 值从一个对象数组移动到另一个对象数组 - Move object value from one array of objects to another 如何在 javascript 中的一个数组中合并另一个数组对象中的数组 - how to merge array inside another array objects in one array in javascript 如何检查数组对象的属性是否与另一个 object 数组中的值之一匹配 - How to check if property of an objects of array matches with one of the values in another array of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM