gpt4 book ai didi

javascript - 抓取另一个对象中的特定对象

转载 作者:行者123 更新时间:2023-11-27 23:33:53 26 4
gpt4 key购买 nike

我有这个js对象

{
"2015": {
"11": {
"10": {
"Family & Friends\nGames": {
"c": 2
},
"Collectible\nGames": {
"c": 1
},
"Logic Games\n": {
"c": 1
},
"Kids Games\n": {
"c": 1
}
},
},
"Family & Friends\nGames": {
"c": 9
},
"Collectible\nGames": {
"c": 2
},
"Logic Games\n": {
"c": 4
},
"Kids Games\n": {
"c": 6
},
"Classic Games\n": {
"c": 3
},
"Preschool\nGames": {
"c": 5
}
},
"meta": {
"id": [
"[CLY]2",
"[CLY]7",
"[CLY]6",
"[CLY]1",
"[CLY]4",
"[CLY]3"
],
"segments": [
"id",
"title"
],
"title": [
"Family & Friends\nGames",
"Collectible\nGames",
"Logic Games\n",
"Kids Games\n",
"Classic Games\n",
"Preschool\nGames"
]
}
}

我试图减去“meta”上方与“title”下的键匹配的相同对象。我使用 ruby​​ 完成了此操作,代码如下

     results = JSON.parse(resp.body)
data = results["2015"]
title = results["meta"]["title"]
alg = data.slice(*title).to_a
info = alg.sort_by { |k,v| v["c"] }

因此,我尝试将对象转换为数组,然后从那里分割与其他数组名称标题匹配的所有内容,最后根据其“c”值对信息进行排序。

这些似乎都不适合我,所以我尝试使用 [] 和内部键的名称从对象中获取数据,但由于数组“title”下的每个名称都有\n 让我必须添加额外的\来访问它。我希望它是动态的,而不必每次在“标题”下有不同的名称时都必须修改它

我需要这样的东西https://jsfiddle.net/7chntms2/6/

感谢您的帮助

最佳答案

这就是你想要实现的目标吗?

var obj = {
"2015": {
"11": {
"10": {
"Family & Friends\nGames": {
"c": 2
},
"Collectible\nGames": {
"c": 1
},
"Logic Games\n": {
"c": 1
},
"Kids Games\n": {
"c": 1
}
},
},
"Family & Friends\nGames": {
"c": 9
},
"Collectible\nGames": {
"c": 2
},
"Logic Games\n": {
"c": 4
},
"Kids Games\n": {
"c": 6
},
"Classic Games\n": {
"c": 3
},
"Preschool\nGames": {
"c": 5
}
},
"meta": {
"id": [
"[CLY]2",
"[CLY]7",
"[CLY]6",
"[CLY]1",
"[CLY]4",
"[CLY]3"
],
"segments": [
"id",
"title"
],
"title": [
"Family & Friends\nGames",
"Collectible\nGames",
"Logic Games\n",
"Kids Games\n",
"Classic Games\n",
"Preschool\nGames"
]
}
}

var keys = obj["meta"]["title"];
var newObj = [];

for (var i = 0; i < keys.length; i++) {
newObj.push({
name: keys[i],
content: obj["2015"][keys[i]]
});
}

newObj.sort(function(a, b) {
if (a.content.c < b.content.c) return -1;
if (a.content.c > b.content.c) return 1;
return 0;
});

console.log(newObj);

关于javascript - 抓取另一个对象中的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295860/

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