0

I have the following array given:

const myArray = [{ id: 1, isSet: true }, { id: 2, isSet: false }, ...];

Actually I want to iterate only the isSet properties of the objects (not all properties). The most simple solution, which came on my mind was the following:

let isSet = false;
for (const obj of myArray) {
    for (const property in obj) {
        if (property === "isSet" && obj.hasOwnProperty(property)) {
            isSet |= obj[property];
        }
    }
}

console.log(isSet);

I think this one does not really look pretty, so has someone a better solution as the given one (maybe also better in runtime)?

Thanks in advance!

12
  • 3
    allAreSet = myArray.every(x => x.isSet) - this? Commented Dec 3, 2018 at 18:53
  • @georg this is a very good solution thx for the hint! Commented Dec 3, 2018 at 18:56
  • Georg's answer is correct, but even w/ manual iteration like you're doing, why wouldn't you just use isSet |= obj.isSet or similar? There's no need to iterate over all the properties in each object in the array. Commented Dec 3, 2018 at 18:56
  • @DaveNewton good hint. I used this syntax, because their could be multiple properties not only the isSet. (The isSet is just a placeholder e.g. it could also be an array with ["isSet", "id"] -> when I want to iterate all properties) Commented Dec 3, 2018 at 18:57
  • 1
    @georg It should be .some() instead of .every(). Based on the above code, I believe OP only cares if any of them are set, not if all of them are set Commented Dec 3, 2018 at 18:59

2 Answers 2

2

You can do this generically if you pass in your set of rules for each property, like so:

const myArray1 = [{ id: 1, isSet: false }, { id: 2, isSet: false }, { id: 3, isSet: false }];

// check if there is an object with id = 3, isSet = true
var conditions = {
  isSet: (obj) => obj.isSet,
  id: (obj) => obj.id === 3
};
// check if there is an object with id = 2, isSet = false
var conditions2 = {
  isSet: (obj) => !obj.isSet,
  id: (obj) => obj.id === 2
};

function testConditions(arr, conditions) {
  // .some() -- do ANY of the objects match the criteria?
  return arr.some(obj => {
    // .every() -- make sure ALL conditions are true for any given object
    return Object.keys(conditions).every(key => {
      // run comparitor function for each key for the given object
      return conditions[key](obj);
    });
  });
}

console.log(testConditions(myArray1, conditions)); // false -- no object with id = 3, isSet = true
console.log(testConditions(myArray1, conditions2)); // true -- myArray1[1] has id = 2, isSet = false

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this looks quite handsome and is exactly what I searched for!
0

You can use the some function of an array.

const myArray1 = [{ id: 1, isSet: false }, { id: 2, isSet: false }, { id: 3, isSet: false }];

let isSet1 = myArray1.some(obj => obj.isSet === true)

console.log(isSet1);

const myArray2 = [{ id: 1, isSet: false }, { id: 2, isSet: true }, { id: 3, isSet: false }];

let isSet2 = myArray2.some(obj => obj.isSet === true)

console.log(isSet2);

3 Comments

this is exactly what .some() is for. var isSet1 = myArray1.some(obj => obj.isSet)
Why create new array with filter()?
woops! I forgot about that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.