简体   繁体   English

比较 object 的两个数组以返回未找到的值

[英]Comparing two array of object to return values that are not found

I have two array of objects and I want to compare them and return values not found in the other我有两个对象数组,我想比较它们并返回在另一个对象中找不到的值

     arr1 = [{ text: "Company", value: 1},{ text: "Project", value: 3},{ 
         text: "Housing" value: 4}]
     arr2 = [{ newCol: "SumAmount", existCol: "Amount"}, { newCol: 
        "SumProj", existCol: "Proj"}]

I am comparing newCol values in arr2 against text in arr1, so since SumAmount and SumProj are not in the text values of arr1 I should get SumAmount and SumProj我将 arr2 中的 newCol 值与 arr1 中的文本进行比较,因此由于SumAmountSumProj不在 arr1 的文本值中,我应该得到SumAmountSumProj

Use filter and some使用filtersome

 arr1 = [{ text: "Company", value: 1},{ text: "Project", value: 3},{ text: "Housing",value: 4}] arr2 = [{ newCol: "SumAmount", existCol: "Amount"}, { newCol: "SumProj", existCol: "Proj"}] var res=arr2.filter(o=>.arr1.some(y=>y.text==o.newCol)) console.log(res)

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

相关问题 比较数组 object 的两个值,然后提醒用户 - Comparing two values of an array object and then alerting user 比较对象javascript的两个数组并删除不相等的值 - Comparing two array of object javascript and remove values that are not equal 将数组值与对象键进行比较 - Comparing Array Values With Object Keys 比较两个对象数组,并使用loadash返回一个具有公共值的数组 - Comparing two arrays of objects and return one array with common values using loadash 比较两个数组并匹配第二个数组对象中第一个数组对象的一个键,如果找到则插入一个键和值 - comparing two arrays and match one key from first array's object in the second array's object if found then insert a key and value 比较JavaScript中对象数组的两个参数 - Comparing two parameters of an object array in JavaScript 比较 JavaScript 中的两个对象数组并通过比较键更新值 - Comparing two array of objects in JavaScript and update the values by comparing Keys 比较两个对象数组并用另一个替换 object - Comparing two array of objects and replacing an object with another 比较Object.values数组不起作用 - Comparing Object.values array not working 比较两个数组值后在数组中获取错误元素 - Getting wrong elements in an array after comparing two array values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM