我如何获得嵌套数组中特定对象的列表?
How would I get a list of specific objects in an nested array?
如果我运行这个:
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
我需要得到所有 categories
我尝试了 item.categories
和
data.parse.pages.map(function(val){
return val.categories[0];
}));
但是不对
假设一页已被return编辑为
Object.values(data.query.pages)[0].categories.map(cat=>cat.title.substr(9))
应该做的工作:
var url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=categories&titles=Victory_Tests&origin=*";
$.getJSON(url,function(data){
var pages = Object.values(data.query.pages);
if(pages.length === 1)
console.log(pages[0].categories.map(cat => cat.title.substr(9)));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
根据您所需的浏览器支持,您可能需要 Object.values
的 polyfill,例如 https://github.com/tc39/proposal-object-values-entries/blob/master/polyfill.js。
(注意更改后的 URL,在我看来,使用 &origin=*
而不是 &callback=?
是更好的方法,因为它不会 return JSONP 但有效 JSON,也可以通过 XMLHttpRequest/fetch
和 JSON.parse
消耗。)
你应该得到不止一页,如果你想汇总所有内容,请这样做:
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);
});
使用API参数formatversion=2
:
var url="https://en.wikipedia.org/w/api.php?format=json&formatversion=2&action=query&prop=categories&titles=Victory_Tests&callback=?";
$.getJSON(url,function(data){
console.log($.map(data.query.pages, function(val){
return val.categories;
}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
(还利用了 jQuery.map
的能力来展平数组。)
如果我运行这个:
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
我需要得到所有 categories
我尝试了 item.categories
和
data.parse.pages.map(function(val){
return val.categories[0];
}));
但是不对
假设一页已被return编辑为
Object.values(data.query.pages)[0].categories.map(cat=>cat.title.substr(9))
应该做的工作:
var url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=categories&titles=Victory_Tests&origin=*";
$.getJSON(url,function(data){
var pages = Object.values(data.query.pages);
if(pages.length === 1)
console.log(pages[0].categories.map(cat => cat.title.substr(9)));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
根据您所需的浏览器支持,您可能需要 Object.values
的 polyfill,例如 https://github.com/tc39/proposal-object-values-entries/blob/master/polyfill.js。
(注意更改后的 URL,在我看来,使用 &origin=*
而不是 &callback=?
是更好的方法,因为它不会 return JSONP 但有效 JSON,也可以通过 XMLHttpRequest/fetch
和 JSON.parse
消耗。)
你应该得到不止一页,如果你想汇总所有内容,请这样做:
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);
});
使用API参数formatversion=2
:
var url="https://en.wikipedia.org/w/api.php?format=json&formatversion=2&action=query&prop=categories&titles=Victory_Tests&callback=?";
$.getJSON(url,function(data){
console.log($.map(data.query.pages, function(val){
return val.categories;
}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
(还利用了 jQuery.map
的能力来展平数组。)