简体   繁体   English

JavaScript 如何通过动态键过滤对象数组?

[英]JavaScript How to filter array of objects by dynamic key?

I have an array which contains objects with dynamic keys, my aim is to find if this array contains or not specific key, How can this be achieved?我有一个包含具有动态键的对象的数组,我的目标是查找该数组是否包含特定键,如何实现?

 let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] let target = 'item1' //result array let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, ]

Use Array#filter() and the in operator使用Array#filter()in运算符

 let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] let target = 'item1' console.log( arr.filter(i => target in i) )

Based on your title, I assume you want to try using array.filter , so follow up your attempt of using array.filter , this is an opion:根据您的标题,我假设您想尝试使用array.filter ,因此请继续尝试使用array.filter ,这是一个选项:

 let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] let target = 'item1' let newarr= arr.filter(each=>Object.keys(each).toString() ==target) console.log(newarr)

JavaScript has built-in functions that return the object's keys for you: JavaScript 具有为您返回对象键的内置函数:

var x = {
    name : "Romulo",
    age : 17
}

console.log(Object.keys(x))

Output: Output:

["name","age"]

This returns an array, you just need check if your key is in it.这将返回一个数组,您只需要检查您的密钥是否在其中。

You can use Array.filter method and Object.hasOwn method for this, please never use the in operator it has some precedence issues.您可以使用 Array.filter 方法和 Object.hasOwn 方法,请不要使用in运算符,它有一些优先级问题。

 const arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] function filterByKey(list, target) { return list.filter( item => Object.hasOwn(item, target) ); } const target = "item1"; console.log( filterByKey(arr, target) );

Already Data已有数据

[
  {item1: { key: 'sdfd', value:'sdfd' }},
  {item2: { key: 'sdfd', value:'sdfd' }},
  {item3: { key: 'sdfd', value:'sdfd' }}
]

Want Data想要数据

Want the data from target , when target is item1 :想要来自target的数据,当 target 是item1时:

[
  {item1: { key: 'sdfd', value:'sdfd' }}
]

Current Code当前代码

 let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] let target = 'item1' // result array let want = [ {item1: { key: 'sdfd', value:'sdfd' }}, ]

The Answer答案

 let arr = [ {item1: { key: 'sdfd', value:'sdfd' }}, {item2: { key: 'sdfd', value:'sdfd' }}, {item3: { key: 'sdfd', value:'sdfd' }} ] let target = 'item1' // result array let want = [ {item1: { key: 'sdfd', value:'sdfd' }}, ] let answer = arr.filter(object => object[target]) console.log('answer:', answer)

Does this answer your question?这回答了你的问题了吗? or if not comment to tell what went wrong.或者如果不发表评论来说明出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM