获取一个列表中不在另一个列表中的项目列表 [英] Get list of items in one list that are not in another

查看:83
本文介绍了获取一个列表中不在另一个列表中的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个以逗号分隔的列表,第一个是可能值列表,第二个是选定值列表。我需要创建第一个列表中第二个列表中不存在的所有项目的列表。

I have two comma separated lists, the first is a list of possible values, and the second is a list of "selected" values. I need to create a list of all of the items in the first list that do not exists in the second list.

我可以将第一个列表拆分为一个数组,使用for通过string_pos查看列表是否包含第二个列表项,但我想知道是否有更有效的方法来完成此任务。

I could just split the first list into an array and use a "for" to go through the list using a string_pos to see if the first list item is contained in the second, but I'm wondering if there is a more efficient way to accomplish this.

谢谢!!

推荐答案

您可以过滤可能的清单。

You can filter the possible list.

如果列表是字符串,则拆分或匹配它们以获取数组。

if the lists are strings, split or match them to get arrays.

var possible=[1,2,3,4],
selected=[2,4];

var unchosen=possible.filter(function(itm){
  return selected.indexOf(itm)==-1;
});

unchosen

/*  returned value: (Array)
1,3
*/

这篇关于获取一个列表中不在另一个列表中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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