JavaScript嵌套对象数组,如何遍历' KEY'阵列?

时间:2017-08-23 16:54:28

标签: javascript arrays javascript-objects

ng-repeat="car in cars | orderBy:'-price' | orderBy:'-stock'"

Ans:汽车
同样,我如何显示数组内的' KEY' 值。 '名称' '型号'

class IndeedSpider(scrapy.Spider):
        name = 'indeed'
        allowed_domains = ['indeed.com'
        ]
        start_urls = ['https://secure.indeed.com/account/login'
        ]

        def parse(self, response):
                return scrapy.FormRequest.from_response( #send a request
                        response,
                        formxpath='//form[@id="signin_email"]', #xpath to the logging form
                        formdata={
                                'password': 'mypassword', #html input type field
                                'email': 'mymail', #other input type field
                                'Action':'/account/login', #form action field
                        },
                        callback=self.after_login #do something                    )

答案:
0 1 1 2

为什么它返回数组索引值,如何显示' KEY'值'名称' '型号'

预期答案:

name      models 
name OR models
name models

3 个答案:

答案 0 :(得分:1)

for循环遍历每个索引,因此您必须获取该索引并钻取到数组

var myCollection = {
"cars": [
          { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
          { "name":"BMW", "models":[ "320", "X3", "X5" ] },
          { "name":"Fiat", "models":[ "500", "Panda" ] }
        ]
}
for ( x in myCollection.cars ) {
  var keys = Object.keys(myCollection.cars[x])
  document.getElementById("id1").innerHTML += keys[0] + "<br />";
}

myCollection.cars [x]是如何获取数组中的特定对象的,并从Object.keys()中获取该对象的键。如果你想要值和键

for ( x in myCollection.cars ) {
  var keys = Object.keys(myCollection.cars[x])
  document.getElementById("id1").innerHTML += 
     keys[0] + " : " + myCollection.cars[x].name + " <br />" +
     keys[1] + " : " + myCollection.cars[x].models + "<hr />";
}

答案 1 :(得分:0)

这个for in对于对象而言是卑鄙的。您正在使用数组,因此返回索引。而是使用如下所示的forEach。

&#13;
&#13;
var myCollection = {
    "cars": [
              { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
              { "name":"BMW", "models":[ "320", "X3", "X5" ] },
              { "name":"Fiat", "models":[ "500", "Panda" ] }
            ]
    }
    myCollection.cars.forEach(function (value) {
      //console.log("name" + value.name + ", models" + value.models);
      //for printing cars alonecars
      console.log(value.name);
      //for print models alone
      value.models.forEach(function (model) {
        console.log(model);
      });
    });
&#13;
&#13;
&#13;

评论下方的行将获得您想要的输出,您可以打印汽车或模型。

答案 2 :(得分:0)

您可以使用解决方案https://jsfiddle.net/sg6kj0n7/1/

x:y:[] -> ...
-- or
[x,y] -> ...
var myCollection = {
"cars": [
          { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
          { "name":"BMW", "models":[ "320", "X3", "X5" ] },
          { "name":"Fiat", "models":[ "500", "Panda" ] }
        ]
}
for ( var x in myCollection.cars ) {
	for (var key in myCollection.cars[x]) {
 	 	document.getElementById("id1").innerHTML += key + "   ";
  }
  document.getElementById("id1").innerHTML += "<br/>";
}