【发布时间】:2021-02-08 16:56:00
【问题描述】:
我正在尝试根据下面给出的数组值过滤对象。我知道如何通过键过滤对象,但在这种情况下,我需要按值过滤它。
这是我的代码:
let _ = [{
"form_id": "659d796d-aa78-4e74-af68-5ef037bb215b",
"form_name": "Investigation Forms",
"form_type": null,
"created_at": "",
"updated_at": ""
},
{
"form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
"form_name": "Other Investigation Forms",
"form_type": {
"form_type_name": "Assessment",
"form_type_id": "5f4419c1-9351-4338-b894-84a000328cc4"
},
"created_at": "",
"updated_at": ""
},
{
"form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
"form_name": "Other/General Investigation Forms",
"form_type": {
"form_type_name": "Report",
"form_type_id": "594ba131-b110-4ec5-855a-9de8440fbc7b"
},
"created_at": "",
"updated_at": ""
}
]
//-------------------------------------------------------------------------------------------------
const values = [0, "5f4419c1-9351-4338-b894-84a000328cc4", "594ba131-b110-4ec5-855a-9de8440fbc7b"]
plz ignore my below code it is work attempt .
const output = _.map(item =>
values.reduce((val, key) => ({
...val,
[key]: item[key]
}), {})
);
console.log(output);
my expected output :
const values = ["5f4419c1-9351-4338-b894-84a000328cc4"]
output = [{
"form_id": "9991f556-fc7f-4400-a491-062b12a0bb03",
"form_name": "Other Investigation Forms",
"form_type": {
"form_type_name": "Assessment",
"form_type_id": "5f4419c1-9351-4338-b894-84a000328cc4"
},
"created_at": "",
"updated_at": ""
}]
form_type 有form_type_id,这是我需要过滤的值。
【问题讨论】:
-
您要根据
form_type. form_type_id的值进行过滤吗? -
@code 是的兄弟,我需要这样的
-
@code 是的,我需要这样
-
你想用什么逻辑来过滤?
form_type_id是否存在,或者它是否具有特定值?
标签: javascript arrays json object