访问键/值对Javascript中的键
如何在javascript中访问键值对的键值 假设函数ruleList创建一个数组,该数组存储ruleResults中每个localizedRuleNames的字符串值?我可以创建一个包含localizedRuleNames的数组 但是如何获取与localizedRuleNames关联的字符串值呢访问键/值对Javascript中的键,javascript,Javascript,如何在javascript中访问键值对的键值 假设函数ruleList创建一个数组,该数组存储ruleResults中每个localizedRuleNames的字符串值?我可以创建一个包含localizedRuleNames的数组 但是如何获取与localizedRuleNames关联的字符串值呢 // Iterate through the localizedRuleNames in ruleResults and // return an array of their strings. f
// Iterate through the localizedRuleNames in ruleResults and
// return an array of their strings.
function ruleList(results) {
// Your code goes here!
ruleArray = [];
for (var i in psinsights.formattedResults.ruleResults){
//console.log(i);
for (var j in psinsights.formattedResults.ruleResults[i]){
// console.log(j);
ruleArray.push(j);
}
}
console.log(ruleArray);
return ruleArray;
}
// Iterate through pageStats in the ps` iResults object and
// return the total number of bytes to load the website.
function totalBytes(results) {
// Your code goes here!
}
// Below, you'll find a sample PS Insights JSON
// and two console.log statements to help you test your code!
psinsights = {
"kind": "pagespeedonline#result",
"id": "/speed/pagespeed",
"responseCode": 200,
"title": "PageSpeed Home",
"score": 90,
"pageStats": {
"numberResources": 22,
"numberHosts": 7,
"totalRequestBytes": "2761",
"numberStaticResources": 16,
"htmlResponseBytes": "91981",
"cssResponseBytes": "37728",
"imageResponseBytes": "13909",
"javascriptResponseBytes": "247214",
"otherResponseBytes": "8804",
"numberJsResources": 6,
"numberCssResources": 2
},
"formattedResults": {
"locale": "en_US",
"ruleResults": {
"AvoidBadRequests": {
"localizedRuleName": "Avoid bad requests",
"ruleImpact": 0.0
},
"MinifyJavaScript": {
"localizedRuleName": "Minify JavaScript",
"ruleImpact": 0.1417,
"urlBlocks": [
{
"header": {
"format": "Minifying the following JavaScript resources could reduce their size by $1 ($2% reduction).",
"args": [
{
"type": "BYTES",
"value": "1.3KiB"
},
{
"type": "INT_LITERAL",
"value": "0"
}
]
},
"urls": [
{
"result": {
"format": "Minifying $1 could save $2 ($3% reduction).",
"args": [
{
"type": "URL",
"value": "http://code.google.com/js/codesite_tail.pack.04102009.js"
},
{
"type": "BYTES",
"value": "717B"
},
{
"type": "INT_LITERAL",
"value": "1"
}
]
}
},
{
"result": {
"format": "Minifying $1 could save $2 ($3% reduction).",
"args": [
{
"type": "URL",
"value": "http://www.gmodules.com/ig/proxy?url\u003dhttp%3A%2F%2Fjqueryjs.googlecode.com%2Ffiles%2Fjquery-1.2.6.min.js"
},
{
"type": "BYTES",
"value": "258B"
},
{
"type": "INT_LITERAL",
"value": "0"
}
]
}
}
]
}
]
},
"SpriteImages": {
"localizedRuleName": "Combine images into CSS sprites",
"ruleImpact": 0.0
}
}
},
"version": {
"major": 1,
"minor": 11
}
};
// Try logging the outputs below to test your code!
console.log(ruleList(psinsights));
console.log(totalBytes(psinsights));
更改:
ruleArray.push(j);
致:
我认为这次更新做到了这一点。希望它能起作用
function ruleList(results) {
ruletArray = [];
for(ruleResult in results.formattedResults.ruleResults){
ruleResultArray.push(results.formattedResults.ruleResults[ruleResult].localizedRuleName);
}
return ruleResultArray;
}
ruleArray.push(psinsights.formattedResults.ruleResults[i][j])
?问题是什么?祖德瓦,你回答了问题。我现在意识到,我应该推动你刚才给的绳子,而不仅仅是“j”。谢谢。
function ruleList(results) {
ruletArray = [];
for(ruleResult in results.formattedResults.ruleResults){
ruleResultArray.push(results.formattedResults.ruleResults[ruleResult].localizedRuleName);
}
return ruleResultArray;
}