gpt4 book ai didi

javascript - 如何获取嵌套数组中特定对象的列表?

转载 作者:行者123 更新时间:2023-11-28 14:48:19 25 4
gpt4 key购买 nike

如果我运行这个:

var url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=categories&titles=Victory_Tests&callback=?";
$.getJSON(url,function(data){
$.each(data, function(i, item) {
console.log(item);
});
});

控制台给我:

{pages: {…}}
pages: 2347232:
categories: Array(8)0:
{ns: 14, title: "Category:Aftermath of World War II in the United Kingdom"}
1: {ns: 14, title: "Category:All articles with unsourced statements"}
2: {ns: 14, title: "Category:Articles with unsourced statements from August 2015"}
3: {ns: 14, title: "Category:Articles with unsourced statements from March 2009"}
4: {ns: 14, title: "Category:English cricket seasons from 1919 to 1945"}
5: {ns: 14, title: "Category:History of Test cricket"}
6: {ns: 14, title: "Category:Use British English from September 2011"}
7: {ns: 14, title: "Category:Use dmy dates from September 2011"}
length: 8
__proto__: Array(0)
ns: 0
pageid: 2347232
title: "Victory Tests"
__proto__:
Object__proto__:
Object__proto__: Object

我需要获取所有类别

我尝试了item.categories

data.parse.pages.map(function(val){ 
return val.categories[0];
}));

但是这是不对的

最佳答案

您可能应该获得多个页面,如果您想聚合所有内容,请执行以下操作:

 var url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=categories&titles=Victory_Tests&callback=?";
$.getJSON(url,function(data){
var categories = Object.values(data.query.pages).map( (i) => i.categories.map( (c) => c.title) );
console.log(categories);
});

关于javascript - 如何获取嵌套数组中特定对象的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533421/

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