0

I have codes :

line:1       var listInput = $('input.model');
line:2       var listVariable = [];
line:3       $.each(listInput, function (index, val) {      
line:4             var variable = $(val).data('name');
line:5             var valOf = $(val).val();
line:6             var item = { variable : valOf };
line:7             listVariable.push(item);
line:8       });

I want the "variable" in line 6 that is the "variable" in line 4

Please help me, sorry about my english.

1
  • Remove line:1 from code Commented Oct 13, 2015 at 9:46

1 Answer 1

1

You need to use Bracket notation to populate item's property using dynamic key.

$.each(listInput, function (index, val) {      
    var item = {}; //Create object
    item[$(val).data('name')] = $(val).val(); //Set the value
    listVariable.push(item);
});

OR, You can use .map() along with .get()

var listVariable = listInput.map(function(){
    var item = {};
    item[$(this).data('name')] = $(this).val();     
    return item;
}).get();
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.