如何将多个对象放在JavaScript中的一个数组中?
如何将多个对象放在JavaScript中的一个数组中?
我有这样的n json字符串:
first object
'[{"name":"1","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"1"}]'
second object
'[{"name":"2","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"2"}]'
third object
'[{"name":"3","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"3"}]'
...
nth object
在这里,我想将n个屈从于
这样的
'[{"name":"1","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"1"},
[{"name":"2","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"2"}],
[{"name":"3","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"3"}]'
结果,我尝试了以下内容,但是如果我推开它,它将进入数组本身。
for (let i = 0; i < DataList.length; i++) {
if (i == 0) {
mergetData = JSON.parse(DataList[0]);
continue;
} else {
mergetData.push(JSON.parse(DataList[i]));
}
}
我想知道如何解决这个问题。
此致!
How to put multiple objects in one array in javascript?
I have n json strings like this:
first object
'[{"name":"1","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"1"}]'
second object
'[{"name":"2","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"2"}]'
third object
'[{"name":"3","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"Platform", "name":"3"}]'
...
nth object
Here I want to put n obejects into one result
like this
'[{"name":"1","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"1"},
[{"name":"2","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"2"}],
[{"name":"3","position":{"x":-28.890169060450745,"y":-89.45,"z":103.39384013230637},"property":{"floor":"승강장","name":"3"}]'
For this, I tried the following, but if I push it, it comes into the Array itself.
for (let i = 0; i < DataList.length; i++) {
if (i == 0) {
mergetData = JSON.parse(DataList[0]);
continue;
} else {
mergetData.push(JSON.parse(DataList[i]));
}
}
I am wondering how can I solve this problem.
Best Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解析所有JSON数组,然后将它们串成一个阵列。
Parse all the JSON arrays, then concatenate them into a single array.
您正在问如何将多个对象放在一个数组中,但是所需的输出是字符串对象。因此,我将所需的输出视为实际问题:)
您只需要通过
Datalist
迭代并将值合并到字符串对象中。或只是
Your are asking how to put multiple objects in one array but your desired output is the string object. So I'm considering your desired output as the actual question :)
For that you just have to iterate through your
DataList
and merge values into an string object.Or just