javascript - 从对象数组中过滤一些对象
问题描述
我有一个对象数组。我正在尝试从数组中过滤一些对象并使用以下内容获取一个新数组。
[
{
"userId": 1,
"title": "title 1",
"body": "Body for user 1",
"address": {
"country": "Germany",
"state": "State1"
},
"phone": 1234
},
{
"userId": 2,
"title": "title 2",
"body": "Body for user 2",
"address": {
"country": "Canada",
"state": "State2"
},
"phone": 4321
}
]
我如何过滤数组并在没有address
and的情况下获取新数组phone
。任何帮助表示赞赏。谢谢
解决方案
您可以使用.map()
和对象解构:
let data = [
{"userId": 1, "title": "title 1", "body": "Body for user 1", "address": {"country": "Germany", "state": "State1"}, "phone": 1234},
{"userId": 2, "title": "title 2", "body": "Body for user 2", "address": { "country": "Canada", "state": "State2"}, "phone": 4321}
];
let result = data.map(({address, phone, ...rest}) => rest);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
文件:
推荐阅读
- python - QTabWidget 不允许我使用自定义类
- javascript - 如何单击下载页面 chrome 中的保留按钮
- python - 嵌套列表的元素总和
- npm - 无法删除赛普拉斯代理设置
- python - 在heroku上部署我的应用程序时出现webbrowser模块错误
- spring - Spring Email:必须先发出 STARTTLS 命令
- node.js - Google Drive API 返回 [对象]
- python - Python While 循环中的初学者问题
- c++ - 是否可以检测 lambda 在捕获组中是否有“this”?
- windows - Win 批量读取文件 - 不将输入保存到其他文件