【发布时间】:2020-02-25 16:48:29
【问题描述】:
我有以下 JsonArray:
arr = [
{
"name": "test",
"alias": "alias1",
"type": 1
},
{
"name": "test",
"type": 0
},
{
"name": "abc",
"alias": "alias2",
"type": 1
}
]
我想使用变量值(可能包含别名/键)来查找。所以基本上find的首选应该是别名,如果没有找到具有相同值的别名,那么它应该在“name”中搜索,而“alias”不存在。
通常情况下会这样:
_.find(arr, {
alias: value
})
但如果没有找到 alias=value,我希望代码返回 name = value 的 obj
1) 例如:值 = "alias1" 预期==>
{
"name": "test",
"alias": "alias1",
"type": 1
}
2) 例如:值 = “测试” 预期==>
{
"name": "test",
"type": 0
}
【问题讨论】:
-
“我有一个以下 JsonArray” - 不,你没有。这是一个对象数组 (What is the difference between JSON and Object Literal Notation?)。
-
我不太清楚这里问的是什么。对于这个示例数组
arr,正确答案是什么?是1吗? -
基本上我想以这样一种方式使用_.find,它首先根据“别名”找到。如果未找到,则查找“名称”
标签: javascript underscore.js lodash