使用lodah
-如何通过其值删除对象?
这是我的尝试:
var array = ['Education', 'Medicine'];
values = [
{
"Category":"Medicine"
},
{
"Category":"Medicine"
},
{
"Category":"Development"
},
{
"Category":"Education"
}
]
const x = _.without(values, array);
console.log(x);期望值为:
values = [{
"Category":"Development"
},]
答案 0 :(得分:1)
您可以使用<li>
<a href="javascript:void(0);" class="menu-toggle"><i class="zmdi zmdi-assignment-o"></i><span>Models</span></a>
<ul class="ml-menu">
@can('index',App\Model1::class)
<li>
<a href="#">{{__('Model1 Manger')}}</a>
</li>
@endcan
@can('index',App\Model2::class)
<li>
<a href="#">{{__('Model2 Manager')}}</a>
</li>
@endcan
</ul>
</li>
删除数组中存在_.differenceWith()
的项目。
Category
const array = ['Education', 'Medicine'];
const values = [{"Category":"Medicine"},{"Category":"Medicine"},{"Category":"Development"},{"Category":"Education"}];
const result = _.differenceWith(values, array, (o, category) => o.Category === category);
console.log(result);
答案 1 :(得分:1)
实际上,我不认为使用lodash会更好。 您可以使用es6来解决它。
result = values.filter(x => array.includes(x.Category));