我有包含以下数据的props
test1: abc
id: 1
myArray: Array(3)
0: {a:abc, b:cde, aid: 1}
1: {e:age, f:ade, aid: 2}
2: {t:are, h:had, aid: 1}
我希望过滤属性并更新数组,使其只包含与id
和aid
匹配的值
因此,道具应该如下所示:
test1: abc
id: 1
myArray: Array(3)
0: {a:abc, b:cde, aid: 1}
2: {t:are, h:had, aid: 1}
我该怎么做呢?
发布于 2018-06-04 08:09:01
您可以使用Array.filter()
// match both a AND aid
const result = myArray.filter(obj => (obj.a === test1 && obj.aid === id);
// match either a OR aid
const result = myArray.filter(obj => (obj.a === test1 || obj.aid === id);
发布于 2018-06-04 09:48:01
您将使用以下命令获得过滤后的数据:
const filteredData = this.props.myArray.filter(item => item.aid === 1)
然而,props只是可读的。你将不得不分派或更新父组件,以提供新的/过滤的数据作为道具。
https://stackoverflow.com/questions/50672010
复制相似问题