javascript - 合并两个json文件并保持相同的结构javascript
问题描述
我在尝试创建包含来自我抓取的网站的信息的 JSON 时遇到了一些问题。我创建了两个文件,并且都具有相同的结构。我想将它们放在一个 JSON 中,并为两者保持相同的结构。我怎样才能做到这一点?
我的文件如下所示:
[
{
name: 'Jed',
age: 23
home: [
{address: 5th AV 123
coordinates: [{lat:12324,
long:1231
}
]
}
]
我有 10 个结构相同但每个文件的信息不同的文件。如何创建一个包含所有 10 个的主要 JSON?
解决方案
您可以使用该函数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; }
推荐阅读
- reactjs - 如何将this.state中的值从一个js文件传递到另一个reactjs
- video - ffmpeg,使用 gpu 降低 fps - 缓冲的数据包过多
- go - Standard_init_linux.go:211: exec 用户进程导致“exec 格式错误”
- python - 如果未设置变量,我如何捕获错误?
- dns - AWS Route 53 路由策略:地理位置与延迟
- reactjs - 在刷新重定向页面时丢失访问令牌
- javascript - 使用 useRef 和窗口调整大小事件(React Hook)的观察者 div 元素大小/维度
- python - 带有 Django 的 AWS Beanstalk:eb create 抱怨未知配置设置“StaticFile”
- node.js - Azure 应用服务上的多个 NodeJS 实例
- bash - 使用 set -e,退出命令以下状态!在 if 块中丢失