使用jQuery或JavaScript过滤数组与另一个数组

时间:2016-06-14 13:58:34

标签: javascript jquery arrays

我正在尝试过滤数组' arrSOPrecods'来自' outputdata'的关键值另一个阵列。第一个阵列' arrSOPrecods'包含用于针对特定SOP进行训练的用户的记录第二个阵列' outputdata'包含已经针对特定SOP培训的用户的记录我将需要过滤掉arrSOprecords中outputdata中存在的记录。我已经尝试了许多不同的方法,例如jQuery过滤器,JavaScript for循环以及它自己的.filter函数,几乎没有进展但没什么用处。这里是数据的示例以及最终输出应该是什么。

var arrSOPrecords = [
    { User: "Cesar", SOP: "training 1" },
    { User: "Cesar", SOP: "training 2" },
    { User: "Jon", SOP: "training 1" },
    { User: "Jon", SOP: "training 2" },
    { User: "Ana", SOP: "training 1" },
    { User: "Ana", SOP: "training 2" }
];

var outputdata = [
    { User: "Cesar", SOP: "training 1" }, 
    { User: "Cesar", SOP: "training 2" }, 
    { User: "Ana", SOP: "training 1" }, 
    { User: "Jon", SOP: "training 1" }
];

最终输出数组。

var filtered = [
    { User: "Ana", SOP: "training 2" },
    { User: "Jon", SOP: "training 2" }
];

6 个答案:

答案 0 :(得分:4)

您可以对项目使用哈希表,然后排除并过滤记录。



var arrSOPrecords = [{ User: "Cesar", SOP: "training 1" }, { User: "Cesar", SOP: "training 2" }, { User: "Jon", SOP: "training 1" }, { User: "Jon", SOP: "training 2" }, { User: "Ana", SOP: "training 1" }, { User: "Ana", SOP: "training 2" }],
    outputdata = [{ User: "Cesar", SOP: "training 1" }, { User: "Cesar", SOP: "training 2" }, { User: "Ana", SOP: "training 1" }, { User: "Jon", SOP: "training 1" }],
    filtered,
    hash = Object.create(null);

outputdata.forEach(function (a) {
    hash[a.User + '|' + a.SOP] = true;
});
filtered = arrSOPrecords.filter(function (a) {
    return !hash[a.User + '|' + a.SOP];
});

console.log(filtered);




答案 1 :(得分:1)

您可以在两个数组上使用map()并返回User + SOP的字符串,然后使用indexOf()过滤



var arrSOPrecords=[{User:"Cesar", SOP:"training 1"},{User:"Cesar", SOP:"training 2"},{User:"Jon", SOP:"training 1"},{User:"Jon", SOP:"training 2"},{User:"Ana", SOP:"training 1"},{User:"Ana", SOP:"training 2"}]; 
var outputdata= [{User:"Cesar", SOP:"training 1"},{User:"Cesar", SOP:"training 2"},{User:"Ana", SOP:"training 1"},{User:"Jon", SOP:"training 1"}]

var a = arrSOPrecords.map(e => e.User+e.SOP);
var b = outputdata.map(e => e.User+e.SOP);

var result = arrSOPrecords.filter(function(e, i) {
  return b.indexOf(a[i]) == -1;
});

console.log(result)




更新:您实际上只需在第二个数组上使用map,然后像这样使用过滤器



var arrSOPrecords = [{ User: "Cesar", SOP: "training 1" }, { User: "Cesar", SOP: "training 2" }, { User: "Jon", SOP: "training 1" }, { User: "Jon", SOP: "training 2" }, { User: "Ana", SOP: "training 1" }, { User: "Ana", SOP: "training 2" }];
var outputdata = [{ User: "Cesar", SOP: "training 1" }, { User: "Cesar", SOP: "training 2" }, { User: "Ana", SOP: "training 1" }, { User: "Jon", SOP: "training 1" }];

var a = outputdata.map(e => e.User+e.SOP);
var result = arrSOPrecords.filter(function(el) {
  return a.indexOf(el.User+el.SOP) == -1;
})

console.log(result)




答案 2 :(得分:1)

Array.prototype.filter() + Array.prototype.some()

var arrSOPrecords = [
    { User: "Cesar", SOP: "training 1" },
    { User: "Cesar", SOP: "training 2" },
    { User: "Jon", SOP: "training 1" },
    { User: "Jon", SOP: "training 2" },
    { User: "Ana", SOP: "training 1" },
    { User: "Ana", SOP: "training 2" }
];

var outputdata = [
    { User: "Cesar", SOP: "training 1" }, 
    { User: "Cesar", SOP: "training 2" }, 
    { User: "Ana", SOP: "training 1" }, 
    { User: "Jon", SOP: "training 1" }
];

var filtered = arrSOPrecords.filter(function(r) {
    return !outputdata.some(function(t) {
        return r.User === t.User && r.SOP === t.SOP;
    });
});

console.log(JSON.stringify(filtered));

答案 3 :(得分:0)

您可以尝试使用array.some方法过滤第一个数组:



var arrSOPrecords=[{User:"Cesar", SOP:"training 1"},{User:"Cesar", SOP:"training 2"},{User:"Jon", SOP:"training 1"},{User:"Jon", SOP:"training 2"},{User:"Ana", SOP:"training 1"},{User:"Ana", SOP:"training 2"}];
var outputdata= [{User:"Cesar", SOP:"training 1"},{User:"Cesar", SOP:"training 2"},{User:"Ana", SOP:"training 1"},{User:"Jon", SOP:"training 1"}]


var r = arrSOPrecords.filter(x => !outputdata.some(y => _.isEqual(y,x)))

console.log(r)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
&#13;
&#13;
&#13;

使用lodash some来比较isEqual方法I中的对象,但您可以使用其他方法来测试对象。<​​/ p>

答案 4 :(得分:0)

以下是使用Array.filterArray.indexOf函数的简单解决方案:

var filtered = [], trained = {};

outputdata.forEach(function(o) {
    trained[o.User] = trained[o.User] || [];
    trained[o.User].push(o.SOP);
});
filtered = arrSOPrecords.filter(function(o) {
    return !trained[o.User] || trained[o.User].indexOf(o.SOP) === -1;
});

console.log(JSON.stringify(filtered, 0 , 4)); 

输出:

[
    {
        "User": "Jon",
        "SOP": "training 2"
    },
    {
        "User": "Ana",
        "SOP": "training 2"
    }
]

答案 5 :(得分:0)

我想为这些问题提供通用的,可重复使用的解决方案。以下代码将列出调用的数组中存在的对象,这些对象在作为参数提供的数组中缺失。我们正在使用两种通用方法; Object.prototype.compare()Array.prototype.diference()

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};
Array.prototype.difference = function(a) {
  return this.filter(e => !a.some(f => f.compare(e)));
};
var arrSOP = [
    { User: "Cesar", SOP: "training 1" },
    { User: "Cesar", SOP: "training 2" },
    { User: "Jon", SOP: "training 1" },
    { User: "Jon", SOP: "training 2" },
    { User: "Ana", SOP: "training 1" },
    { User: "Ana", SOP: "training 2" }
],
   outData = [
    { User: "Cesar", SOP: "training 1" }, 
    { User: "Cesar", SOP: "training 2" }, 
    { User: "Ana", SOP: "training 1" }, 
    { User: "Jon", SOP: "training 1" }
],
  filtered = arrSOP.difference(outData);
console.log(JSON.stringify(filtered));