How can I pare a property value in multiple objects of an array?
I have a few objects in array x
.
var arr = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, ...]
I want to pare and return true if the value of 'a'
is same in all the objects in the array
How can I pare a property value in multiple objects of an array?
I have a few objects in array x
.
var arr = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, ...]
I want to pare and return true if the value of 'a'
is same in all the objects in the array
-
Use every,
array.every(el => el.a === 1)
. – rmn Commented Sep 13, 2018 at 19:02 - So, what's the problem? This seems pretty trivial to do, what specific issue are you facing? – VLAZ Commented Sep 13, 2018 at 19:02
- I actually want to return true if the value of 'a' is same in all objects in the array – Chris Jake Commented Sep 13, 2018 at 20:22
- @VLAZ there's no need to be unsupportive. I think that the question is very clear. – RandomDeveloper Commented Jul 16, 2021 at 8:53
-
@Rpx at the time I had left the ment, the question was different. It asked to check if
a
is1
in the entire array. It was later clarified that OP wants to check ifa
is the same regardless of what the value is. – VLAZ Commented Jul 16, 2021 at 10:26
4 Answers
Reset to default 4For checking if all objects contains for the same key the same value, you could use a destructuring assignment for getting the first item and check against the actual item.
var array = [{ a: 1, b: 2, c: 3, d: 4 }, { a: 1, x: 2, y: 3, z: 4 }],
key = 'a';
console.log(array.every((a, _, [b]) => a[key] === b[key]));
Taking a substring for pairing
var array = [{ a: 12345, b: 2, c: 3, d: 4 }, { a: 12367, x: 2, y: 3, z: 4 }];
console.log(array.every((a, _, [b]) => a.a.toString().slice(0, 3) === b.a.toString().slice(0, 3)));
ES5
var array = [{ a: 12345, b: 2, c: 3, d: 4 }, { a: 12367, x: 2, y: 3, z: 4 }];
console.log(array.every(function (a, _, b) {
return a.a.toString().slice(0, 3) === b[0].a.toString().slice(0, 3);
}));
I would use Array.reduce:
const data1 = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}];
const data2 = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, {a: 2}];
data1.reduce((acc, item) => acc && item.a === 1, true); // true
data2.reduce((acc, item) => acc && item.a === 1, true); // false
For getting true
if the value of a
is same in all objects in the array I would use Array.prototype.some
with simple inversion:
!data1.some((item, index, list) => item.a !== list[index && 1 - 1].a); // true
!data2.some((item, index, list) => item.a !== list[index && 1 - 1].a); // false
Here I'm looking for the first item in the list which has a
property that does not match a
property of the previous item. Zero-indexed item will be skipped, it will be pared with itself (index && 1 - 1
is just 0
if index is 0
). The result is being inverted with !
, so I will have true
if no item is found.
I would simple use Array.prototype.every()
to test if all the objects have a
as 1
.
var arr = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, {a:1, x:2, y:3, z:4}];
let result = arr.every((obj)=> obj.a === 1);
console.log(result);//true
arr = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, {a:2, x:2, y:3, z:4}];
result = arr.every((obj)=> obj.a === 1);
console.log(result);//false
//Checks whether all the a keys is same in the array of objects.
result = arr.every((obj)=> obj.a === arr[0].a);
console.log(result); //false
arr = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, {a:1, x:2, y:3, z:4}];
result = arr.every((obj)=> obj.a === arr[0].a);
console.log(result); //true
Seems like you could use Array.prototype.every() for this.
It allows you to run a test function over each element in an array and return true if every element in the array passes, and return false otherwise.
EDIT: If you wanted to check that all objects have property a
and all values are equal you could do the following:
const x = [{a:1, b:2, c:3, d:4}, {a:1, x:2, y:3, z:4}, {a:2, g:33, f:100}];
let condition = x.every( elem => elem.a == x[0].a);
You can also use Array.prototype.some() to return true or false if any one of the elements passes the test.
Here's a fiddle that shows the case