Reputation: 423
Hi i m trying to iterate the objects using object.keys where i want to ignore the first object and loop through the remaining items or obejcts
below is the object structure:
controls = {
101: {
value: true
},
131: {
value: false
},
134: {
value: false
},
136: {
value: false
},
123: {
value: false
},
}
here is the code which i am trying to loop and get final result:
const value = Object.keys(controls).forEach((k, i) => {
if (i > 0) {
return controls[k].value === false;
}
})
console.log(value, 'result')
The output should be: -
value should return true --> if all the value are false else return true if one of the object is true, except the first value i.e is id: 101
Upvotes: 0
Views: 1537
Reputation: 11156
Ciao, try something like this:
let obj = {
101: {
value: true
},
131: {
value: false
},
134: {
value: false
},
136: {
value: false
},
123: {
value: false
},
}
let result = false;
Object.keys(obj).forEach((k, i) => {
if (k !== "101") result = result || obj[k].value;
})
console.log(result)
A forEach on all keys and a bool result
in which store value
values.
Upvotes: 1
Reputation: 6714
Depending on implementation order of keys of objects may not be guaranteed so you should be extra careful with it. Assuming that order is what you want it to be, you could use slice
and every
or some
array methods.
const obj = {
101: {
value: true
},
131: {
value: false
},
134: {
value: false
},
136: {
value: false
},
123: {
value: false
},
}
const result = !Object.values(obj).slice(1).every(({value}) => !value);
const result2 = Object.values(obj).slice(1).some(({value}) => value);
console.log({result, result2})
Upvotes: 4