Why this is not Working in Javascript
.......
........
key1 = 'start' + i + '';
key2 = 'end' + i + '';
datePass.push({
key1: start,
key2: end
});
it returns
[{key1 : `somevalu开发者_运维百科e`, key1: 'somevalue'}]
but i want
[{start1 : `somevalue`, end1: 'somevalue'}]
and so on.
How can i do so. pleas help.
try using:
datePass[key1] = 'some value';
datePass[key2] = 'some other value';
try this
var i=1;
var key1 = 'start' + i;
var key2 = 'end' + i;
var datePass = {}; // this instantiates a blank object
datePass[key1] = 'someValue'; // assigns a value to object key
datePass[key2] = 'someValue';
IF your goal is to create a list of {startx: xxx, endx: xxx}
object s , since you're using push:
you've to proceed in the following way.
key1 = 'start' + i;
key2 = 'end' + i;
var o = {};
o[key1] = start;
o[key2] = end;
datePuss.push(o);
精彩评论