jQuery-从对象获取数组

时间:2018-09-28 09:22:56

标签: javascript jquery

我的变量输出如下-

console.log(arrayForPrice);

93: {id: "93", code: "color", label: "Color", options: Array(3), position: "1"}
141: {id: "141", code: "size", label: "Size", options: Array(3), position: "0"}

关于Json字符串化-

{
"93":{"id":"93","code":"color","label":"Color","options":[{"id":"50","label":"Blue","products":["1216","1219","1222"]},{"id":"58","label":"Red","products":["1217","1220","1223"]},{"id":"60","label":"Yellow","products":["1218","1221","1224"]}],"position":"1"},
"141":{"id":"141","code":"size","label":"Size","options":[{"id":"168","label":"S","products":["1216","1217","1218"]},{"id":"169","label":"M","products":["1219","1220","1221"]},{"id":"170","label":"L","products":["1222","1223","1224"]}],"position":"0"}
}

我需要将另一个变量的输出与上面共享的两个对象的键进行匹配。但是我面临的问题是我无法破解并进入密钥内部。我想要键options

中的数组

我正在尝试的代码如下-

$.each(arrayForPrice,function(i) {

    $.each(data.programs[i], function (key, val) {
        console.log(key);
    });
});

但是它不起作用。如何进入选项数组?

4 个答案:

答案 0 :(得分:1)

在javascript中,您可以访问像这样的元素

 for(var key in arrayForPrice){
  var obj = arrayForPrice[key];
  var optionArray = obj.options;
  optionArray.forEach(function(data){
   // Access the elements are 
  }); 
 }

答案 1 :(得分:0)

也许您会得到帮助

var data = {
"93":{"id":"93","code":"color","label":"Color","options":[{"id":"50","label":"Blue","products":["1216","1219","1222"]},{"id":"58","label":"Red","products":["1217","1220","1223"]},{"id":"60","label":"Yellow","products":["1218","1221","1224"]}],"position":"1"},
"141":{"id":"141","code":"size","label":"Size","options":[{"id":"168","label":"S","products":["1216","1217","1218"]},{"id":"169","label":"M","products":["1219","1220","1221"]},{"id":"170","label":"L","products":["1222","1223","1224"]}],"position":"0"}
}



$.each(data,function(index,val) {
    console.log(val.options); //Here you can go inside options
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

答案 2 :(得分:0)

var arrayForPrice = {
"93":{"id":"93","code":"color","label":"Color","options":[{"id":"50","label":"Blue","products":["1216","1219","1222"]},{"id":"58","label":"Red","products":["1217","1220","1223"]},{"id":"60","label":"Yellow","products":["1218","1221","1224"]}],"position":"1"},
"141":{"id":"141","code":"size","label":"Size","options":[{"id":"168","label":"S","products":["1216","1217","1218"]},{"id":"169","label":"M","products":["1219","1220","1221"]},{"id":"170","label":"L","products":["1222","1223","1224"]}],"position":"0"}
};


for(var i in arrayForPrice){
	console.log(arrayForPrice[i].options);
}

答案 3 :(得分:-1)

for(var i = 0; i < arrayForPrice.length; i++){
    var priceItem = arrayForPrice[i];
    for(var key in priceItem){
        console.log(key);
    }
}

以上是一种访问所有密钥的方法。

要访问options数组,我会选择类似的东西:

for(var i = 0; i < arrayForPrice.length; i++){
     var priceItem = arrayForPrice[i];
     if(typeof priceItem['options'] != 'undefined'){
         var optionsArray = priceItem['options'];
     }
}