0

I am trying to find out if two required properties are found in array of objects, I need a boolean that says true if at least those items are found and false if any of them is missing, any other properties are not an issue for the boolean as long as those two required properties are there.

So I got this, but I don't know how to produce a true boolean when both files are included

const data = [
  {
    name: 'required1'
  },
  {
    name: 'noRequired'
  },
  {
    name: 'required2'
  },
  {
    name: 'noRequired'
  },
]

const required = ['required1', 'required2']

const result = data.filter(({ name }) => !required.includes(name))


console.log(result)  
.as-console-wrapper { max-height: 100% !important; top: 0; }

I have tried some and every but it does not work as it has to be the two included in the array as in 'every' but not the rest in the array as in 'some'

3 Answers 3

3
function containsAll(needles, haystack){ 
  for(var i = 0; i < needles.length; i++){
     if($.inArray(needles[i], haystack) == -1) return false;
  }
  return true;
}

containsAll([34, 78, 89], [78, 67, 34, 99, 56, 89]); // true
containsAll([34, 78, 89], [78, 67, 99, 56, 89]); // false
containsAll([34, 78, 89], [78, 89]); // false
Sign up to request clarification or add additional context in comments.

5 Comments

It does not work, if you remove required2 from data you still get true
you need to check if required array data available or not in data array right ?? so if required2 remove then required1 is still available in data's array and you can use here forEach loop also instead of filter
['required1', 'required2' ] both need to be there, if so is true, regardless of the orher values. If any of those strings are missing, it should be false.
okk got you wait i'll do some changes in code
please use now if still not working then tell me
2

every does work, but you have to create a new array of names, and then check to see if those names are included in your required list. You can map over an array of objects to create an new array of names and use that for a comparison.

const data=[{name:"required1"},{name:"noRequired"},{name:"required2"},{name:"noRequired"}];

const required = ['required1', 'required2'];

const values = data.map(obj => obj.name);

const result = required.every(name => {
  return values.includes(name);
});

console.log(result);

3 Comments

I like this approach but why not just do const values = data.map(({name}) => name)? and then just const result = required.every(name => values.includes(name))
yeah, data is an array so Object.values(data) gives a shallow copy of the array which makes no use here.
Sorry! I've been up all night. You're both right. Sometimes I'm a moron.
1

You can just check the difference between 2 arrays (required - data) and if the length is 0 then true else false

const data = [{
    name: 'required1'
  },
  {
    name: 'noRequired'
  },
  {
    name: 'required2'
  },
  {
    name: 'noRequired'
  },
]

const required = ['required1', 'required2']

const names = data.map(m => m.name);
const result = !required.some(f => !names.includes(f));

// Alternative -
//const diffCount = required.filter(f => !names.includes(f)).length;
//const result = diffCount == 0;


console.log(result)

2 Comments

!some(!condition) is the same as every(condition)
@marzelin, yes I realize that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.