[英]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
(任何)项目是否与requiredfileds
的item
具有相同的名称。
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.