过滤器两种不同的结构化阵列强调JS [英] Filter two different structured arrays underscore js

查看:126
本文介绍了过滤器两种不同的结构化阵列强调JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

 数组1 = [{名称:'ABC',ID:23},{名称:'某某',ID:10},{名称:'高清',ID:12} ];
数组2 = [10,23]。

合成数组应该是阵列1的ID与数组2的内容相交的一部分。

下面,其结果必然是结果= [{名称:'ABC',ID:23},{名称:'某某',编号:10}] ;

任何想法,我可以如何使用下划线JS实现这一目标?


解决方案

  _过滤(数组1,函数(项目){返回_.contains(数组2,item.ID);} );

您可以使用过滤并的包含

试试看这里

I have two arrays:

array1 = [{Name: 'abc', ID: 23},{Name:'xyz', ID: 10},{Name:'def', ID: 12}];
array2 = [10,23];

Resultant array should be part of array 1 whose ID intersects with contents of array2.

Here, the result would be result = [{Name: 'abc', ID: 23},{Name: 'xyz', ID:10}];

Any ideas how I could achieve this using underscore js?

解决方案

_.filter(array1, function(item){ return _.contains(array2, item.ID); });

You can use filter and contains.

Try it out here!

这篇关于过滤器两种不同的结构化阵列强调JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆