简体   繁体   中英

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'},

So i have tried (with 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. This callback function tests if some (any) of the items in results has the same name as the item in requiredfileds .

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).

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 .

 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:

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 :

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. Combine with Array.some should give you what you want.

 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM