如何在javascript中将对象添加到对象中

时间:2016-08-15 08:10:58

标签: javascript

我知道有push()将oject添加到数组中。但是如何将对象添加到对象中。

var osubcategories = {};

for (var i=0; i<data.length; ++i){
      var tempkey = data[i].scid;   // here tempkey will be any number sat 15 20 30 etc

      // how to add this tempkey along with true in osubcategories
 }

我希望循环中的每个tempkey都有这样的输出:

osubcategories = {"15" : true, "23": true, "55" : true}

1 个答案:

答案 0 :(得分:1)

var osubcategories = {};
for (var i=0; i<data.length; ++i){
  var tempkey = data[i].scid;   // here tempkey will be any number sat 15 20    30 etc
  // the following line will add the properties to the "osubcategories" object
  osubcategories[tempKey] = true;
}