最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript check if each element of an object array is contained in another array - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Jun 19, 2018 at 16:41 GeoffGeoff 6,66924 gold badges99 silver badges214 bronze badges 3
  • Try using .every(). – Barmar Commented Jun 19, 2018 at 16:42
  • @Barmar .every returns true if all values pass a test, I don't think that would give the op what they're looking for. – zfrisch Commented Jun 19, 2018 at 16:45
  • Didn't see that he also wanted the list of missing names, rather than just a true/false result. – Barmar Commented Jun 19, 2018 at 16:48
Add a ment  | 

5 Answers 5

Reset to default 4

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

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

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

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

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

const missing = requiredFields.filter(({name}) => !entered.includes(name))
发布评论

评论列表(0)

  1. 暂无评论