[英]How to filter JSON Object (JavaScript)
I am trying to filter the below JSON object and return an array of objects where the value within the key of 'markdown' contains say "steve" - to do this I suspect I need to convert the object to an array then filter.我正在尝试过滤下面的 JSON object 并返回一个对象数组,其中“markdown”键中的值包含“steve” - 为此,我怀疑我需要将 ZA8CFDE6331BD59EB2AC96F8911111 数组转换为过滤器。
I have tried using Object.entries
我试过使用
Object.entries
Object.entries(diagnosisData).filter(item => item === 'steve')
as well as JSON.parse
but think I am barking up the wrong tree.以及
JSON.parse
但认为我在吠叫错误的树。
I'd like to return say:我想回来说:
result = [
{
"id": "stevey",
"markdown": "STEVEY",
"source": null
},
{
"id": "steven",
"markdown": "STEVEN",
"source": null
}
]
Could anyone offer a pointer for me?谁能给我指点一下?
Many thanks非常感谢
JSONdata = {
"steven": {
"id": "steven",
"markdown": "STEVEN",
"source": null
},
"john": {
"id": "johnA",
"markdown": "JOHNA",
"source": null
},
"henry": {
"id": "henryP",
"markdown": "HENRYP",
"source": null
},
"stevel": {
"id": "steveL",
"markdown": "STEVEL",
"source": null
}
}
Try to use Object.entries
:尝试使用
Object.entries
:
let filterKey = 'steve';
const result = Object.entries(JSONdata).filter(([k, v]) => k== filterKey);
and then Object.fromEntries()
to create an object from a list of key-value pairs:然后
Object.fromEntries()
从键值对列表中创建 object :
Object.fromEntries(result)
An example:一个例子:
let JSONdata = { "steve": { "id": "steve", "markdown": "STEVE", "source": null }, "john": { "id": "john", "markdown": "JOHN", "source": null }, "henry": { "id": "henry", "markdown": "HENRY", "source": null }, }; let filterKey = 'steve'; const result = Object.entries(JSONdata).filter(([k, v]) => k == filterKey); console.log(Object.fromEntries(result))
UPDATE:更新:
You can use startsWith
method to get desired result:您可以使用
startsWith
方法来获得所需的结果:
const result = Object.values(JSONdata).filter((v) =>
v.id.startsWith(filterKey));
An example:一个例子:
let JSONdata = { "steveN": { "id": "steven", "markdown": "STEVEN", "source": null }, "john": { "id": "johnA", "markdown": "JOHNA", "source": null }, "henry": { "id": "henryP", "markdown": "HENRYP", "source": null }, "steveL": { "id": "steveL", "markdown": "STEVEL", "source": null } } let filterKey = 'steve'; const result = Object.values(JSONdata).filter((v) => v.id.startsWith(filterKey)); console.log(result)
You are almost there with your Object.entries(...).filter
approach.您的
Object.entries(...).filter
方法几乎就在那里。 However, I'm not sure what you want to filter by - the keys, the id
values or the markdown
values?但是,我不确定您要过滤什么 - 键、
id
值或markdown
值?
To avoid confusion, let's say you have an object like this instead:为避免混淆,假设您有这样的 object:
const data = {
keyA: {
id: 'idA',
markdown: 'markdownA'
},
keyB: {
id: 'idB',
markdown: 'markdownB'
}
}
Then, just for reference, the Object.XX
functions yield these results:然后,仅供参考,
Object.XX
函数产生以下结果:
console.log(Object.keys(data))
// ['keyA', 'keyB']
console.log(Object.values(data))
// [
// {id: 'idA', markdown: 'markdownA'},
// {id: 'idB', markdown: 'markdownB'}
// ]
console.log(Object.entries(data))
// [
// ['keyA', {id: 'idA', markdown: 'markdownA'}],
// ['keyB', {id: 'idB', markdown: 'markdownB'}]
// ]
So:所以:
To filter by the key, there is no filter needed at all as long as it's a perfect match your are looking for:要按键过滤,根本不需要过滤器,只要它是您正在寻找的完美匹配:
const result = data.keyA
console.log(result) // {id: 'idA', markdown: 'markdownA'}
If needed a non-exact match though, say for example all keys ending with A
, you can use Object.entries(...).filter
(and then map
to the value):如果需要不完全匹配,例如所有以
A
结尾的键,您可以使用Object.entries(...).filter
(然后map
到值):
const result = Object.entries(data)
.filter(([key, value]) => key.endsWith('A'))
.map(([key, value]) => value)
console.log(result) // [{id: 'idA', markdown: 'markdownA'}]
To filter by one of the properties ( id
or markdown
), you could use Object.entries(...).filter
, but since you are then not even interested in the key, you could use Object.values(...).filter
instead:要按属性之一(
id
或markdown
)进行过滤,您可以使用Object.entries(...).filter
,但由于您甚至对密钥不感兴趣,您可以使用Object.values(...).filter
代替:
const result = Object.values(data).filter(item => item.id === 'keyA')
console.log(result) // [{id: 'idA', markdown: 'markdownA'}]
One way to do it is: Object.values(JSONdata).filter(o => o.id === 'steve')
一种方法是:
Object.values(JSONdata).filter(o => o.id === 'steve')
Or if you do not have the is and want to do it by key:或者,如果您没有 is 并想通过密钥进行操作:
const key = Object.keys(JSONdata).filter(o => o === 'steve');
console.log(JSONdata[key]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.