Reputation: 7112
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!
Upvotes: 1
Views: 7388
Reputation: 8936
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>)}
Upvotes: 1
Reputation: 4982
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>
);
}
Upvotes: 0
Reputation: 6684
filter
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)
Upvotes: 5