简体   繁体   English

Javascript检查对象数组的每个元素是否包含在另一个数组中

[英]Javascript check if each element of an object array is contained in another array

I ave two arrays with objects in them 我有两个包含对象的数组

var requiredfileds=[
    {name:'CVR', value:'cvr_code'},
    {name:'NODE POINT VAL', value:'node_point_val'},

 ]

The second array 第二个数组

var results = [
    {name:'CVB', data:[12,11,233,445]}
    {name:'CVR', data:[125,1,-45,4]}
   ]

Now i want to check to ensure that all the names in the required fields array are in the results array. 现在我想检查以确保所需字段数组中的所有名称都在结果数组中。 So from the above two examples i expect it to be false and get the required field missing to be {name:'NODE POINT VAL', value:'node_point_val'}, 所以从上面两个例子中我认为它是假的并且缺少必需的字段{name:'NODE POINT VAL', value:'node_point_val'},

So i have tried (with es6) 所以我试过(用es6)

this.is_valid = true;   
this.requiredfields.forEach(field=>{
  this.results.forEach(item=>{
    this.is_valid = item.name === field.name;
  })
})

But the above doesnt work, How do i proceed 但上述不起作用,我该如何进行

Try this: filter the requiredfileds by applying a callback function. 试试这个:通过应用回调函数filter requiredfileds This callback function tests if some (any) of the items in results has the same name as the item in requiredfileds . 此回调函数测试results中的some (任何)项目是否与requiredfiledsitem具有相同的名称。

The result in missing is the filtered array which did not fulfilled the criterion (the one which are present in requiredfileds and not present, by name, in the results array). missing的结果是未满足标准的过滤数组(存在于requiredfileds且在名称中不存在于results数组中的标准)。

If you simply want to know whether there were missing values or not you can just check the missing array length like this: !!missing.length . 如果您只是想知道是否存在缺失值,您可以像这样检查missing数组长度: !!missing.length

 var requiredfileds = [{ name: 'CVR', value: 'cvr_code' }, { name: 'NODE POINT VAL', value: 'node_point_val' }, ]; var results = [{ name: 'CVB', data: [12, 11, 233, 445] }, { name: 'CVR', data: [125, 1, -45, 4] }]; var missing = requiredfileds.filter(item => !results.some(resultItem => resultItem.name === item.name)); console.log('Any missing: ' + !!missing.length); console.log(missing); 

IMHO, you want something like this perhaps: 恕我直言,你想要这样的东西:

 var requiredfileds=[ {name:'CVR', value:'cvr_code'}, {name:'NODE POINT VAL', value:'node_point_val'} ] var results = [ {name:'CVB', data:[12,11,233,445]}, {name:'CVR', data:[125,1,-45,4]} ] var resultsNames = results.map(result => result.name) requiredfileds.forEach(requiredfiled => { if(!resultsNames.includes(requiredfiled.name)) console.log(requiredfiled) }) 

ES6 one-liner with boolean result value: 带有布尔结果值的ES6单行:

const result = !requiredfileds.some(i => !results.some(j => j.name === i.name));

A version with first item that does not satisfy the requirement: 第一个项目不满足要求的版本:

const badItem = requiredfileds.find(i => !results.some(j => j.name === i.name));

If you want to have an array of wrong items, replace .find with .filter : 如果您想要一个错误项目数组,请将.find替换为.filter

const badItems = requiredfileds.filter(i => !results.some(j => j.name === i.name));
const entered = results.map(({ name }) => name);

const missing = requiredFields.filter(({name}) => !entered.includes(name))

As @Barmer suggested... you could use Array.every. 正如@Barmer建议的......你可以使用Array.every。 Combine with Array.some should give you what you want. 与Array.some结合应该可以满足您的需求。

 var requiredFields=[ {name:'CVR', value:'cvr_code'}, {name:'NODE POINT VAL', value:'node_point_val'}, ]; var results = [ {name:'CVB', data:[12,11,233,445]}, {name:'CVR', data:[125,1,-45,4]} ]; const answer = requiredFields.every(item => results.some(test => item.name === test.name)) console.log(answer); 

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

相关问题 检查数组是否包含在另一个数组中 - Check if a array is contained in another 检查对象数组中的每个 object 是否包含另一个数组中的值(JavaScript) - Check if each object in array of objects contains a value that is in another array (JavaScript) 通过另一个数组中包含的对象属性对Javascript数组进行排序 - Ordering a Javascript array by object properties contained in another array 一种检查值是否包含在Javascript数组(而不是整个对象)中的干净方法 - Clean way to check if a value is contained within a Javascript Array (not the entire object) 检查另一个数组中是否包含一个数组 - Check if one array is contained in another array 使用JavaScript检查元素是否在对象内包含的数组内 - Checking if an element is inside an array contained within an object using JavaScript Javascript检查数组中的每个元素 - Javascript check against each element in an array 如果对象包含在另一个数组中,则从数组中删除它 - Remove object from array if it is contained in another array JavaScript 如何用另一个数组的每个元素替换数组的第一个元素 - JavaScript How to replace first element of an array with each element of another array Javascript:检查数组元素是否包含来自另一个数组的元素 - Javascript : Check array element contains element from another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM