我在尝试创建包含我抓取的网站信息的 JSON 时遇到了一些问题。我创建了两个文件,它们都具有相同的结构。我想将它们放在一个 JSON 中,并为两者保持相同的结构。我该怎么做?
我的文件如下所示:
[
{
name: 'Jed',
age: 23
home: [
{address: 5th AV 123
coordinates: [{lat:12324,
long:1231
}
]
}
]
我有 10 个结构相同但每个文件的信息不同。如何创建包含所有 10 个的主 JSON?
您可以创建如下内容:
MainJson = [
[
[Information From Site -1 related to A],
[Information From Site -2 related to A]
],
[
[Information From Site -1 related to B],
[Information From Site -2 related to B]
]
]
可以使用函数Array.prototype.concat
将对象追加到特定数组。 同样,使用 Spread 语法将元素作为参数传递。
let array = anArray.concat(...anotherArray);
或按如下方式传递多个数组
let anArray = [{ name: 'Jed', age: 23, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}, { name: 'Ele', age: 36, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}],
anotherArray = [{ name: 'Rick', age: 21, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}, { name: 'Jade', age: 42, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}],
furtherArray = [{ name: 'Enr', age: 21, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}, { name: 'John', age: 42, home: [{ address: "5th AV 123", coordinates: [{ lat: 12324, long: 1231 }] }]}];
console.log(Array.prototype.concat.call(anArray, ...anotherArray, ...furtherArray));
.as-console-wrapper { max-height: 100% !important; top: 0; }