简体   繁体   English

签入数组 ids 等于派发 id 值

[英]Check in an array ids are equals to dispatch the id value

I create an array of ids value mapping an existing array我创建了一个映射现有数组的 ids 值数组

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));从这里我想检查 ids 数组的每个项目的 id 值是否相同(就像在这个例子中一样)我想在存储中调度 id 值(dispatch(setIdValue(value));

How can i do this check?我该怎么做这个检查?

You're looking for the .every() Javascript Array method.您正在寻找.every() Javascript 数组方法。

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))
}

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.我会使用每个 function 来检查 id 的相等性,如果迭代中的所有元素都满足条件,则此 function 返回 true。

 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") }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM