0

How do I write this?

chatBox_array.push({key:val_d65f});

They 'key', somehow gets to just "key":value, for objects instead of the value inside the key. It becomes sometime like the following.

0: Object
key: "false"
__proto__: Object
1: Object
key: "blue"
__proto__: Object

Here's the full code.

var chatBox_vars = [{'return_url':chatBox_return_url},{'base_color':chatBox_base_color}, 
{'mid_color':chatBox_mid_color}, 
{'init_open':chatBox_init_open},
{'init_open':chatBox_mx_logo}, 
{'welcome_msg':chatBox_welcome_msg},    
{'clientid':chatBox_clientid}];

    var chatBox_array = [];

    for (var i=chatBox_vars.length;i--;){
            var item_6dds3=chatBox_vars[i];

            for (var key in item_6dds3) {
                if (Object.prototype.hasOwnProperty.call(item_6dds3, key)) {
                var val_d65f = item_6dds3[key];
                    if (val_d65f == null || val_d65f == 'undefined' || val_d65f == ''){} else {
                        console.log(key);
                        chatBox_array.push({key:val_d65f}); 
                    }   
                }
            }
    }
user2864740
  • 60,010
  • 15
  • 145
  • 220
Relm
  • 7,923
  • 18
  • 66
  • 113

1 Answers1

2

Try creating an object before setting property

var obj = {}; obj[key] = val_d65f;
chatBox_array.push(obj);
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    Substituted using bracket notation to set property using reference to variable `key` for setting "key" as property for each object literal created . See http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object/ – guest271314 Jan 05 '16 at 07:49