过滤掉对象中的特定键 [英] filtering out specific keys in an object

查看:71
本文介绍了过滤掉对象中的特定键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写出对象的键/值对.我正在尝试过滤出对象中的2个键,如下所示:

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>)}

这里是"paramas"中的对象之一:

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回调必须是一个函数"

"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?

谢谢!

推荐答案

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)

这篇关于过滤掉对象中的特定键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆