在对象内部搜索键

search for key inside object

提问人:user3653474 提问时间:11/17/2023 更新时间:11/17/2023 访问量:72

问:

 {
    "id":805,
    "name":'test',
    "Email":[
    {
    "Name":"name1",
    "Email":"[email protected]"
    },
    {
    "Name":"name2",
    "Email":"[email protected]"
    }
    ..
    ..
    ],
    "createdDate":"..."
    },
    {
    "id":806,
    "name":'test',
    "Email":[
    {
    "Name":"name1",
    "Email":"[email protected]"
    },
    {
    "Name":"name2",
    "Email":"[email protected]"
    }
    ..
    ..
    ],
    "createdDate":"..."
    }





 this.data=this.data.filter((item=> this.selectedId.includes(item.id)));

其中 this.selectedId = ['805','806']

let email='[email protected]';

如果包含此内容,那么我们还需要显示该数据。Email[email protected]

 this.data=this.data.filter((item=> this.selectedId.includes(item.id)) || Object.keys(item.Email).includes(email));
JavaScript 角度

评论

0赞 Selaka Nanayakkara 11/17/2023
你能详细说明一下你的问题吗?
0赞 user3653474 11/17/2023
@SelakaNanayakkara我在电子邮件变量中有一封电子邮件,我也想在电子邮件数组对象中搜索它,如果它在那里我们需要该数据,第一个条件是工作(item=> this.selectedId.includes(item.id))
0赞 Titus 11/17/2023
尝试:。如果两个条件都需要为真,请使用this.data.filter((item => this.selectedId.includes(item.id)) || item.Email.some(o => o.Email === email));&&||

答:

0赞 Ranjeet Sharma 11/17/2023 #1

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}


const map = {"first" : "1", "second" : "2"};
console.log(getKeyByValue(map,"2"));

0赞 Yong Shun 11/17/2023 #2

lambda 表达式/函数的语法不正确。

虽然您可以使用双否定 () 来指示过滤器返回值。.find()!!Email

this.data = this.data.filter(item => this.selectedId.includes(item.id)
    || !!item.Email.find(x => x.Email == this.email));

或者用.findIndex()

this.data = this.data.filter(item => this.selectedId.includes(item.id)
    || item.Email.findIndex(x => x.Email == this.email) > -1);

请注意,使用 ,您将收到不兼容错误,因为数组中提供的 s 是 。selectedId = ['805','806']iddataNumber