gpt4 book ai didi

javascript - 分解大型 JSON 对象

转载 作者:太空宇宙 更新时间:2023-11-04 16:29:54 25 4
gpt4 key购买 nike

我有很多文件需要上传到数据库,我通过浏览器执行此操作,但服务器在发送给它的数据方面存在大小问题,因此拒绝了它。在这种情况下,服务器不受我的控制,因此我需要先将此对象分解为更小的对象,然后再将它们发送到服务器以插入数据库(如果重要,请重新考虑数据库)。它们不需要插入的顺序,我只需要把文件分成更小的部分。

我知道我需要一个循环,但我不明白如何循环遍历 100 个对象,将它们保存为另一个对象,将它们发送到服务器,然后从 101 开始,依此类推......

循环遍历我得到的所有对象,但我无法弄清楚或找到示例。

下面的代码可以达到其目的,但是它只到达 250 然后停止,它不会超过这个......

var lineData = jsonData.theData;
var newData = [];
var newLine;
//Shows on the browser console how many objects there are in the data
console.log(jsonData.theData.length);

//Goes through the Data and grabs the first 1000 lines
for(var i = 0; i< 250; i++){
newLine = lineData[i]
newData.push(newLine);
console.log(lineData[i]);
}

最佳答案

您需要做的就是循环遍历对象,并为每 100 个对象推出新数组并重新开始。所以类似:

var lineData = jsonData.theData;
var newData = [];

for (var i=0; i < lineData.length; i++) {
newLine = lineData[i];
newData.push(newLine);
if (i && !(i % 100)) {
// upload newData
newData = [];
}
}
// check if there's any data left
if (newData.length) {
// upload the remaining data
}

关于javascript - 分解大型 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39905513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com