javascript - 如何从 JSON Node.js 获取特定的数据值?
问题描述
这是我的 JSON 文件输出:
let employees = [{
"id":1,
"name":"Lann",
"username":"brot",
"email":"b@sd.com",
"address": {
"city":"Gweh",
"zipcode":"92998-3874",
"geo": {
"lat":"45",
"lng":"77"
}
}
}]
我如何从中获取 id、姓名和电子邮件,如下所示:
{
"id":1,
"name":"Lann",
"email":"b@sd.com"
}
解决方案
您可以使用地图存档。
let employees = [{
"id":1,
"name":"Lann",
"username":"brot",
"email":"b@sd.com",
"address":{
"city":"Gweh",
"zipcode":"92998-3874",
"geo":{
"lat":"45",
"lng":"77"
}
}
}]
const data = employees.map(o => ({ id: o.id, name: o.name, email:o.email }));
console.log(data[0]);
推荐阅读
- here-api - 如何找到两个位置之间的道路距离?
- c# - 使用 ASP.NET Core 使用 async 和 await 进行异步编程
- swift - Swift 用子协议中的具体类型替换关联类型
- javascript - Ember 更新页面标题
- xml - 如何在 XSD 中定义本地类型?
- javascript - 千位逗号分隔符问题仅验证最后 3 位数字
- java - BulkOperations upsert 抛出无效的 BSON 字段名称
- datastage - IBM DataStage:ODBC_Connector_0:模式协调检测到列的大小不匹配
- python - 如何在 macOS 上编译 Python 3.8 这个二维码
- laravel - Laravel 命令每周运行 2 天