我有如下功能:
getValue(param) {
var result = [];
result = this.state.ObjectsData.map(x => {
if (result.indexOf(x.param) === -1) {
result.push(x.param)
}
});
}
//ObjectsData is array of objects
ObjectsData = [
{tag: 23, name: vicky, gender: m},
{tag: 13, name: sam, gender: m},
{tag: 23, name: raj, gender: m}
]
//x can be any object like below:
x = {tag: 23, name: vicky, gender: m}
param是propertyName like tag
即使param在map之前和之后的map之后有一些值,我也会在下面的行中找到未定义的param
if (result.indexOf(x.param) === -1) { }
我正在尝试为任何属性找到唯一值,例如: 对于标记:23,13。是的,x.tag或x.name或x.gender工作正常,但我想使propertyName动态
答案 0 :(得分:3)
您想要动态检查名称为param
的值的键,而不是
if (result.indexOf(x.param) === -1) {
result.push(x.param)
}
您要检查名为param
的密钥
if (result.indexOf(x[param]) === -1) {
result.push(x[param])
}
使用param
答案 1 :(得分:0)
ObjectsData = [
{tag: 23, name: 'vicky', gender: 'm'},
{tag: 13, name: 'sam', gender: 'm'},
{tag: 30, name: 'raj', gender: 'm'}
]
result = _.map(ObjectsData,'tag'); //[23, 13, 30], use indexof result!
if(result.indexOf(23)===-1)
result = 'not found';
else
result = 'found';