繁体   English   中英

将新对象添加到数组

[英]Add new object to array

我正在努力将一个新对象推入一个数组。 目前,下面的代码似乎只是覆盖了一个对象。 我可以看到我想要通过控制台推送到数组的数据,但它只是没有存储新对象。 有任何想法吗?

fs.readFile('check.json', function (err, check) {
    if (err) throw err;
    var newData = JSON.parse(check);

    var tempData =[];
    for (var index=0; index<newData.length; index++){
        tempData.push(newData);
        }
    tempData = newData;
});

迭代数组然后将对象推送到它们的典型方法就是这样

var tempData =[];
for (var index=0; index<newData.length; index++){
    tempData.push(newData[index]);
    //                     ^add index
}
//tempData = newData; remove assignment which overwrites array

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM