![](/img/trans.png)
[英]How to check if in an array of objects two equal properties are repeated at the same time?
[英]Check if two array are equal and disable a button of array that have same value
我正在渲染卡片并且我有两个数组,我想禁用两个数组之间具有相同 ID 的按钮。
例如:
idOne = [9,5,1,4];
idTwo = [6,1,3,4];
在这种情况下,我想禁用 4 和 1 的按钮,因为它们具有相同的值。
这是我传递按钮的渲染方法
render(){
this.button()
}
这是我的尝试
button = () {
idOne = [9,5,1,4];
idTwo = [6,1,3,4];
const checkId = idOne.some(n => idTwo.includes(n))
if(checkedId){
return <Button disable />
}
return <Button />
}
}
但这将禁用所有按钮,而不是具有相同 ID 的按钮。
谢谢您的帮助
我把它分成两个单独的步骤。 首先,我将使用过滤器返回一个包含两个 id 之间匹配的所有 id 的数组。
const arr1 = [9, 5, 1, 4]
const arr2 = [6, 1, 3, 4]
const matches = arr1.filter(el => arr2.includes(el)
然后,一旦我有了所有匹配项的数组,您就可以对 arr1 中的所有 id 进行map
,然后检查是否有任何 id 同步。 这将返回一个布尔值,允许您渲染条件组件
return arr1.map(el => {
if (matches.includes(el)) {
return <Button disable />
}
return <Button />
})
如果您愿意,可以通过链接过滤器和 map 将其重构为更小到 1 步
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.