具有成员名称的唯一数组

时间:2019-01-25 07:09:43

标签: javascript arrays unique

我有一个具有组成员(last_name,first_name)的数组,但是在组“ Testgroup”中是另一个组“ Testgroup2”。不好的是,我没有从两个组中获得唯一的值。

有人可以帮忙吗?它是Javascript

var manager_list = new Array;
var manager_list = new Array;
var srvData = new spl_srv();


    <LIST PREFIX=list WHERE="group.last_name = 'Testgroup'">
        var wc = "group = U'$list.member'";
        var data = srvData.read("grpmem", wc, "member.last_name|member.first_name|member");
        var anz  = srvData.getcount();
        if(anz > 0) {
            for (i=0; i<anz; i++){
                var UU_ID = srvData.getdata(i,2);
                UU_ID = UU_ID.replace(/\r?\n|\r/g,"");
                manager_list.push(srvData.getdata(i,0)+', '+srvData.getdata(i,1)+'@,@'+UU_ID);
            }
        } 
        else {

        manager_list.push("$list.member.last_name, $list.member.first_name@,@$list.member");
        }

3 个答案:

答案 0 :(得分:2)

有多种方法可以从数组中获取唯一值(删除重复项)

  1. var uniqueManagers = []; $.each(manager_list, function(i, el){ if($.inArray(el, uniqueManagers) === -1) uniqueManagers.push(el); });

  2. let uniqueManagers = Array.from(new Set(manager_list))

您的代码应类似于

var manager_list = new Array;
var manager_list = new Array;
var srvData = new spl_srv();
       <LIST PREFIX=list WHERE="group.last_name = 'Testgroup'">
        var wc = "group = U'$list.member'";
        var data = srvData.read("grpmem", wc, "member.last_name|member.first_name|member");
        var anz  = srvData.getcount();
        if(anz > 0) {
            for (i=0; i<anz; i++){
                var UU_ID = srvData.getdata(i,2);
                UU_ID = UU_ID.replace(/\r?\n|\r/g,"");
                manager_list.push(srvData.getdata(i,0)+', '+srvData.getdata(i,1)+'@,@'+UU_ID);
            }
        } 
        else {

        manager_list.push("$list.member.last_name, $list.member.first_name@,@$list.member");
        }
  manager_list = Array.from(new Set(manager_list))

答案 1 :(得分:0)

我想您应该展平数组然后过滤:

const data = yourArray.reduce(
  (acc, current) => acc.concat(current), 
[]);
  
const uniqueItems = [...new Set(data)]; 

答案 2 :(得分:0)

单线简单实用的解决方案,它使用standardbuilt-in函数,而不是自行开发的解决方案。

var manager_list = ['smith', 'john', 'phil', 'john'];
var unique = manager_list.filter(function(item, i, arr){ return arr.indexOf(item) === i; });

// result = ['smith', 'john', 'phil'];