Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  180] [ 2]  / answers: 1 / hits: 37234  / 14 Years ago, sun, november 7, 2010, 12:00:00

I am using this slice of code (shown below) in an attempt to populate the object literal named Parameters inside the for loop. I need the key:value pair to be assigned in conjunction with the loops iterating i variable, as such: {key_1:chunks[1],key_2:chunks[2]}. However, my code isn't working. The 'key_'+i is not being reflected in the literal.



There's something I am missing here, obviously. Can someone tell me what it is?...Thanks.



var Parameters=[];
var len = chunks.length;
for (var i = 0; i < len; i++) {
var key='key_'+i
obj= { key : chunks[i]};
Parameters.push(obj)
}

More From » javascript

 Answers
16

EDIT: Use var obj = {}; obj[key] = chunks[i];



Because ECMAScript treats the key in this {key:1} as literal.


[#95048] Thursday, November 4, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;