Braeden Orchard
Braeden Orchard

Reputation: 245

Changing 'name' property of jquery object

I'm having an issue with a Javascript object. When I use push on the object, both parts should take on the value of a variable. For some reason though, only valueOfName seems to change based on the variable. I need name to change as well, at the moment it seems to take whatever value it is written as with or without quotes. Here is the relevent code.

var obj = {};
obj.videoID = [];
var terms = ['best','awesome','incredible','insane','great'];
$.each(terms, function(cur, val){
    $.each(data.items, function(i, object) {
        $.each(object.id, function(property, value) {
            obj.videoID.push({val:value});
        });
    });
});

Am I going about this wrong? Or is there something I've missed? Thanks in advance for your help guys.

Upvotes: 0

Views: 51

Answers (1)

Satpal
Satpal

Reputation: 133403

Create a object and use Bracket notation to create property with dynamic name, afterwards push() the object to array.

$.each(object.id, function(property, value) {
    var dummy = {}; //Create object
    dummy[val] = value; //create property and set value
    obj.videoID.push(dummy);
});

Upvotes: 1

Related Questions