[英]String (split and loop ) to Array
can someone tell me where the below code is missing to get the correct data.. 有人可以告诉我下面的代码在哪里缺失以获取正确的数据。
var temprule="MO,WE";
var rulest =[];
var jsondata=[];
rulest=temprule.split(',');
console.log("TTTTT rulest", rulest);
for(var j = 0; j < rulest.length; j++)
{
var day=rulest[j];
entry.day=day;
console.log(entry.day) // log shows MO and WE as per loop
jsondata.push(entry); // but jsondata has 2 entries with day WE
}
incorrect o/p ->[{day=WE},day=WE}] expected--> [{day=MO},day=WE}]
The variable entry needs to be initialized inside the loop. 变量条目需要在循环内初始化。
var temprule="MO,WE";
var rulest =[];
var jsondata=[];
rulest=temprule.split(',');
console.log("TTTTT rulest", rulest);
for(var j = 0; j < rulest.length; j++)
{
var entry={};
var day=rulest[j];
entry.day=day;
console.log(entry.day) // log shows MO and WE as per loop
jsondata.push(entry); // but jsondata has 2 entries with day WE
}
console.log(JSON.stringify(jsondata));
Here is the jsfiddle link with corrected code. 这是带有正确代码的jsfiddle链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.