枚举任意对象键 - Javascript
Enumerating arbitrary object keys - Javascript
美好的一天,
我从维基百科解析这个结果时遇到了问题。
{
"batchcomplete": "",
"query": {
"pages": {
"252408": {
"pageid": 252408,
"ns": 0,
"title": "Bulacan",
"extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
}
}
}
但它创建了一个随机数密钥“252408”,我想在不声明随机数密钥和提取密钥的情况下解析 "extract" 密钥的值。
所以,如果"pages"
键下的对象永远只有一个键,而你不知道它的值,你可以使用,
var randKeyObj = Object.keys(obj.query.pages)[0];
这将始终获得传入对象的第一个或唯一键。
您可以使用Object.values()
访问随机键对应的值。
var res = {"batchcomplete": "","query":{"pages":{"252408": {"pageid": 252408,"ns": 0,"title": "Bulacan","extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"}}}};
let result = Object.values(res.query.pages)[0].extract;
console.log(result);
您可以执行 var keys = Object.keys(batchcomplete.query.pages);
之类的操作来获取随机密钥 (keys[0]),然后您可以使用该密钥提取提取物 属性.
也可以使用for-in
循环遍历对象
var wiki = {
"batchcomplete": "",
"query": {
"pages": {
"252408": {
"pageid": 252408,
"ns": 0,
"title": "Bulacan",
"extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
}
}
}
}
for(var key in wiki["query"].pages){
console.log(key);
}
这是页面ID,不是随机数。您可以使用 formatversion=2
API 参数来获取普通数组。
美好的一天,
我从维基百科解析这个结果时遇到了问题。
{
"batchcomplete": "",
"query": {
"pages": {
"252408": {
"pageid": 252408,
"ns": 0,
"title": "Bulacan",
"extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
}
}
}
但它创建了一个随机数密钥“252408”,我想在不声明随机数密钥和提取密钥的情况下解析 "extract" 密钥的值。
所以,如果"pages"
键下的对象永远只有一个键,而你不知道它的值,你可以使用,
var randKeyObj = Object.keys(obj.query.pages)[0];
这将始终获得传入对象的第一个或唯一键。
您可以使用Object.values()
访问随机键对应的值。
var res = {"batchcomplete": "","query":{"pages":{"252408": {"pageid": 252408,"ns": 0,"title": "Bulacan","extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"}}}};
let result = Object.values(res.query.pages)[0].extract;
console.log(result);
您可以执行 var keys = Object.keys(batchcomplete.query.pages);
之类的操作来获取随机密钥 (keys[0]),然后您可以使用该密钥提取提取物 属性.
也可以使用for-in
循环遍历对象
var wiki = {
"batchcomplete": "",
"query": {
"pages": {
"252408": {
"pageid": 252408,
"ns": 0,
"title": "Bulacan",
"extract": "Bulacan (Tagalog: Lalawigan ng Bulacan; Kapampangan: Lalawigan ning Bulacan) (PSGC: 031400000; ISO: PH-BUL) is a province in the Philippines, located in the Central Luzon Region (Region III) in the island of Luzon, 11 kilometres (6.8 mi) north of Manila (the nation's capital), and part of the Metro Luzon Urban Beltway Super Region. Bulacan was established on August 15, 1578.\nIt has 569 barangays from 21 municipalities and three component cities (Malolos the provincial capital, Meycauayan, and San Jose del Monte). Bulacan is located immediately north of Metro Manila. Bordering Bulacan are the provinces of Pampanga to the west, Nueva Ecija to the north, Aurora and Quezon to the east, and Metro Manila and Rizal to the south. Bulacan also lies on the north-eastern shore of Manila Bay.\nIn the 2015 census, Bulacan had a population of 3,292,071 people, the highest in Region III and the 2nd most populous in the Philippines. Bulacan's most populated city is San Jose del Monte, the most populated municipality is Santa Maria while the least populated is Doña Remedios Trinidad.\nIn 1899, the historic Barasoain Church in Malolos was the birthplace of the First Constitutional Democracy in Asia.\n\n"
}
}
}
}
for(var key in wiki["query"].pages){
console.log(key);
}
这是页面ID,不是随机数。您可以使用 formatversion=2
API 参数来获取普通数组。