gpt4 book ai didi

Javascript 多维数组键值对

转载 作者:行者123 更新时间:2023-11-30 15:34:58 25 4
gpt4 key购买 nike

我正在对 JSON 执行 AJAX 请求并获得以下代码作为响应:

{
total: "1",
items: [
{
id: 43,
title: "ThisIsTheTitle",
promoted: false,
sticky: false,
weight: 10,
created: {
timestamp: 1482054,
formatted: "17/01/2017"
},
url: "http://...",
airdate: {
timestamp: 1484980,
formatted: "17/01/2017"
},
video: {
id: 43,
number_of_views: 1,
duration: {
seconds: 50,
formatted: "00:00"
},
required_login: false
},
program: {
id: 25,
url: "http://...",
title: "ProgrammaTitel"
},
image: {
uri: "public://...",
full: "http://..."
},
tags: [
{
id: "4",
name: "Map"
},
{
id: "7",
name: "Aflevering2"
}
]
}
]
}

现在我将这些数据推送到我自己的 JSArray 中。请注意,现在只有 1 个响应项,但会添加更多。

我想根据对象标签的名称检索特定对象数据 (item > tags > name = 'Aflevering2')

所以我想要标签名称为 'Aflevering2' 的对象中的数据。

有什么建议吗?谢谢!

最佳答案

您可以通过 filter() 的组合找到项目和 some() :

obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));

let obj = {
total: "1",
items: [
{
id: 43,
title: "ThisIsTheTitle",
promoted: false,
sticky: false,
weight: 10,
created: {
timestamp: 1482054,
formatted: "17/01/2017"
},
url: "http://...",
airdate: {
timestamp: 1484980,
formatted: "17/01/2017"
},
video: {
id: 43,
number_of_views: 1,
duration: {
seconds: 50,
formatted: "00:00"
},
required_login: false
},
program: {
id: 25,
url: "http://...",
title: "ProgrammaTitel"
},
image: {
uri: "public://...",
full: "http://..."
},
tags: [
{
id: "4",
name: "Map"
},
{
id: "7",
name: "Aflevering2"
}
]
}
]
}

let items = obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));
console.log(items);

关于Javascript 多维数组键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41694057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com