0

I want to create a object with a specific name and an array, (e.g. myObject = { name : array()}). I have this code :

 $(".selectedQueues").each(function(){
            var selectionName = $(this).find("input").val();
            var containedQueues = new Array();
            $(this).find("li").each(function(){
                containedQueues.push($(this).text());
            });
            var object = new Object();
            object.selectionName = containedQueues;
            console.log(object);
        });

And when I print the object it is like {selectionName : array[]} , but I want it to be {the value in selectionName : array[]}, can someone help me?

1
  • 1
    Remember, objects can be accessed with [] as well as .. object[selectionName] = containedQueues; Commented Aug 28, 2013 at 18:18

1 Answer 1

1

Try changing object.selectionName to object[selectionName]:

$(".selectedQueues").each(function(){
    var selectionName = $(this).find("input").val();
    var containedQueues = new Array();
    $(this).find("li").each(function(){
        containedQueues.push($(this).text());
    });
    var object = new Object();
    object[selectionName] = containedQueues;
    console.log(object);
});

jsFiddle example

EDIT: I was unsure if you were looking to just use selectionName, or the actual name property, which you need to change that portion to:

var selectionName = $(this).find("input").attr('name');
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.