-3

I am aware there is push() to add oject into array. But how to add object into object.

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
 }

I want output like this for each tempkey in loop:

osubcategories = {"15" : true, "23": true, "55" : true}
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
  • 1
    Possible duplicate of [How can I add a key/value pair to a JavaScript object?](http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object) – Harkirat Saluja Aug 15 '16 at 08:13
  • `osubcategories[tempkey] = true;` in loop – coyer Aug 15 '16 at 08:13

3 Answers3

2

How about osubcategories[tempkey] = true;

EDIT: forgot tempkey

Pep.
  • 360
  • 4
  • 9
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;
}
Naga Srinu Kapusetti
  • 1,563
  • 15
  • 12
0

how about this..

var osubcategories = {};
var data = [15,20,30];
var tempkey = {};
for (var i=0; i<data.length; ++i){
      osubcategories[data[i]] = true;
}
console.log(osubcategories);
cakpep
  • 87
  • 6