如何将多个对象放在JavaScript中的一个数组中?

发布于 2025-02-09 18:11:22 字数 1255 浏览 1 评论 0原文

如何将多个对象放在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最单纯的乌龟 2025-02-16 18:11:22

解析所有JSON数组,然后将它们串成一个阵列。

let parsedData = Datalist.map(s => JSON.parse(s));
let mergeData = [].concat(...parsedData);

Parse all the JSON arrays, then concatenate them into a single array.

let parsedData = Datalist.map(s => JSON.parse(s));
let mergeData = [].concat(...parsedData);
旧时浪漫 2025-02-16 18:11:22

您正在问如何将多个对象放在一个数组中,但是所需的输出是字符串对象。因此,我将所需的输出视为实际问题:)

您只需要通过Datalist迭代并将值合并到字符串对象中。

StringObject = ''
for(let i=0; i < DataList.length; i++){
    if(i===0)
        StringObject += DataList[i]
    else
        StringObject += ',' + DataList[i]
}
console.log(StringObject)

或只是

StringObject = String(DataList)
console.log(StringObject)

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.

StringObject = ''
for(let i=0; i < DataList.length; i++){
    if(i===0)
        StringObject += DataList[i]
    else
        StringObject += ',' + DataList[i]
}
console.log(StringObject)

Or just

StringObject = String(DataList)
console.log(StringObject)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文