gpt4 book ai didi

Lodash 在数组中查找数组

转载 作者:行者123 更新时间:2023-12-05 03:02:25 25 4
gpt4 key购买 nike

我有一个如下所示的数组:

   var roles = [ 
{ "label": "Super Auditor", "value": 4 },
{ "label": "Super Finance Officer", "value": 3 },
{ "label": "Super Manager", "value": 2 },
{ "label": "Super Admin", "value": 1 }
]

我需要查找它是否在数组中并获取该对象。

var needToFind = [4, 1]

预期结果:

var results =[
{ "label": "Super Auditor", "value": 4 },
{ "label": "Super Admin", "value": 1 }
]

我只是不知道该怎么做。 TY

最佳答案

您可以使用 _.intersectionWith() :

var roles = [ 
{ "label": "Super Auditor", "value": 4 },
{ "label": "Super Finance Officer", "value": 3 },
{ "label": "Super Manager", "value": 2 },
{ "label": "Super Admin", "value": 1 }
]

var needToFind = [4, 1]

var result = _.intersectionWith(roles, needToFind, (a, b) => a.value === b)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

关于Lodash 在数组中查找数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com