Javascript check if each element of an object arra

2020-03-24 08:14发布

问题:

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

回答1:

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



回答2:

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


回答3:

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



回答4:

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



回答5:

const entered = results.map(({ name }) => name);

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