admin管理员组文章数量:1401626
I am writing out the key/value pair of an object. I'm trying to filter out 2 of the keys in the object like this:
{Object.entries(params.char).filter('Attribute1').filter('Attribute2').map(([key,value], index) => <Text key={key}>{key}</Text>)}
Here is one of the objects in "paramas":
{
id: 1,
Name: "Drax Bravesword",
Rating: "*****",
XP: 392
Age: 34
Avatar: require('./images/profile1.png'),
Map: require('./images/map_1.png'),
Attribute1: "Power",
Attribute2: "Knowledge",
Attribute3: "Family"
}
However when I run this, I get this error:
"Array.prototype.filter callback must be a function"
I'm not quite sure how to fix this, but is there a way to fix it or am I doing this all wrong?
Thanks!
I am writing out the key/value pair of an object. I'm trying to filter out 2 of the keys in the object like this:
{Object.entries(params.char).filter('Attribute1').filter('Attribute2').map(([key,value], index) => <Text key={key}>{key}</Text>)}
Here is one of the objects in "paramas":
{
id: 1,
Name: "Drax Bravesword",
Rating: "*****",
XP: 392
Age: 34
Avatar: require('./images/profile1.png'),
Map: require('./images/map_1.png'),
Attribute1: "Power",
Attribute2: "Knowledge",
Attribute3: "Family"
}
However when I run this, I get this error:
"Array.prototype.filter callback must be a function"
I'm not quite sure how to fix this, but is there a way to fix it or am I doing this all wrong?
Thanks!
Share Improve this question edited Jun 8, 2017 at 16:16 SkyeBoniwell asked Jun 8, 2017 at 15:30 SkyeBoniwellSkyeBoniwell 7,13215 gold badges100 silver badges218 bronze badges3 Answers
Reset to default 5filter
array method accepts function as argument, not string. So please use this:
Object.entries(params.char).filter((item) => (item.indexOf('Attribute1') || item.indexOf('Attribute2') > -1 ))...
let ob = {
Attribute1: 11,
Attribute2: 22,
Attribute3: 33
};
console.log('Not filtered', Object.entries(ob));
let result = Object.entries(ob).filter((item) => (item.indexOf('Attribute1') > -1 || item.indexOf('Attribute2') > -1 ));
console.log('Filtered:', result)
From what I'm understanding, you want to only make text elements for attributes 1 and 2, you won't have to chain the filters...you can do it in one go:
{Object.entries(params.char).filter(touple =>
(touple[0] === 'Attribute1' || touple[0] === 'Attribute2')).map(touple => <Text key={touple[0]}>{touple[0]}</Text>)}
You almost did it by yourself :)
Here is the answer:
render() {
const textElements = Object.entries(params.char)
.filter(([key]) => key !== 'Attribute1' && key !== 'Attribute2')
.map(([key, value]) => <Text key={key}>{key}, {value}</Text>);
return (
<View>
{textElements}
</View>
);
}
本文标签: javascriptfiltering out specific keys in an objectStack Overflow
版权声明:本文标题:javascript - filtering out specific keys in an object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744280025a2598605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论