I create an array of ids value mapping an existing array
const ids = array.map(item => ({ id: item.homologationId }));
This is the result
ids =[{ "id": "15079"},{"id": "15079"},{"id": "15079"},{"id": "15079"},]
From here i want to check if for each item of the ids array the id values are the same ( like in this example ) i want to dispatch in the store the id value (dispatch(setIdValue(value));
How can i do this check?
CodePudding user response:
You're looking for the .every() Javascript Array method.
Something like this will do the trick.
const ids = array.map(item => ({ id: item.homologationId }));
const [head] = ids
if (head && ids.every(item => item.id == head.id)) {
dispatch(setIdValue(head.id))
}
CodePudding user response:
I would use every function to check the equality of the ids, this function returns true if the all the elements in an iterable fulfills a condition.
const ids =[{ "id": "15079"},{"id": "15079"},{"id": "15079"},{"id": "15079"}]
const element = ids.at(0);
if(ids.every(x => x.id === element.id)) {
console.log("All the elements are equals")
} else {
console.log("Not all the elements are equals")
}