下划线 JS 查找对象
Underscore JS find Object
我有一个如下的数组
var goal = [
{
"category" : "education",
"title" : "Charlie University",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2020",
"value" : 50000,
"achievability" : 3,
"experimental_achievability": 3,
"designation": "{"id":1, "jobCode":1003}",
"suggested": false,
"accounts": [],
"articles": [],
"related_goals": [],
"id":"1"
},
{
"category" : "education",
"title" : "Charlie University",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2020",
"value" : 50000,
"achievability" : 3,
"experimental_achievability": 3,
"designation": "{"id":2, "jobCode":1004}",
"suggested": false,
"accounts": [],
"articles": [],
"related_goals": [],
"id":"2"
},
]
如何使用下划线 JS 从目标数组中获取具有 属性 "designation": "{"id":2, "jobCode":1004}",
的对象。
我可以使用 _.findWhere 吗?
你不需要下划线,你可以使用 Array.find
goal.find(function(item) {
return item.id === 2 && item.jobCode === 1004
})
es2015 示例:
goal.find(item => item.id === 2 && item.jobCode === 1004)
我有一个如下的数组
var goal = [
{
"category" : "education",
"title" : "Charlie University",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2020",
"value" : 50000,
"achievability" : 3,
"experimental_achievability": 3,
"designation": "{"id":1, "jobCode":1003}",
"suggested": false,
"accounts": [],
"articles": [],
"related_goals": [],
"id":"1"
},
{
"category" : "education",
"title" : "Charlie University",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2020",
"value" : 50000,
"achievability" : 3,
"experimental_achievability": 3,
"designation": "{"id":2, "jobCode":1004}",
"suggested": false,
"accounts": [],
"articles": [],
"related_goals": [],
"id":"2"
},
]
如何使用下划线 JS 从目标数组中获取具有 属性 "designation": "{"id":2, "jobCode":1004}",
的对象。
我可以使用 _.findWhere 吗?
你不需要下划线,你可以使用 Array.find
goal.find(function(item) {
return item.id === 2 && item.jobCode === 1004
})
es2015 示例:
goal.find(item => item.id === 2 && item.jobCode === 1004)