0

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.

1 Answer 1

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.