简体   繁体   English

具有很少字段的javascript数组比较

[英]javascript arrays with few fields compare

I have two arrays (data and data_not_included).Each elemet of those arrays has attridutes id and name. 我有两个数组(data和data_not_included),这些数组的每个元素都有属性ID和名称。 I fill them this way: 我以这种方式填写他们:

data[i] = {
           name :products.models[i].get('name'),
           id :  products.models[i].get('id')
          };

Now I want do display the elements in data which are not in data_not_included array. 现在,我想显示不在data_not_included数组中的数据中的元素。 For example I have 例如我有

data=[{name: Sugar}{id: 1},{name: Butter}{id: 2},{name: Cola}{id: 3}]
// and
data_nat_included = [{name: Sugar}{id: 1},{name: Butter}{id: 2}].

It should display {name: Cola}{id: 3} only. 它应仅显示{name: Cola}{id: 3}

Here is what I have already done: 这是我已经做的:

for(var j=0;j<data_not_icluded.length;j++)
{
    for(var i=0;i<data.length;i++)
    {
        if(data[i].id != data_not_icluded[j].id ){
          //but this doesnt work for me it displayes a lot of element many times
        }
     }
}

Both answers are asymptotically bad. 这两个答案都是渐近的。 This means they run in suboptimal time. 这意味着它们运行时间不理想。 In other words, they are naive approaches to solving the problem. 换句话说,它们是解决问题的幼稚方法。 This problem is more widely known in the domain of databases, where join operation is a commonplace. 在联接操作很常见的数据库领域中,此问题已广为人知。 It is also known that the complexity of a join is O(log n * n + log m * m) where n is the number of elements in first table and m is the number of elements in the second table. 还已知联接的复杂度为O(log n * n + log m * m) ,其中n是第一个表中的元素数, m是第二个表中的元素数。 This is fewer operations then would be required by naive solution offered in other examples O(n^2) . 这比其他示例O(n^2)提供的天真的解决方案所需的操作要少。

However, if more is known about your data, as, for example, I would expect that the values are unique and easily serializable to string, you could even reduce the complexity to O(n + m) by simply creating hashes of the objects you want to compare. 但是,如果您对数据有更多的了解,例如,我希望这些值是唯一的,并且可以轻松地序列化为字符串,那么您甚至可以通过简单地创建对象的哈希来将复杂度降低到O(n + m) 。想比较。 Here's how to do it: 方法如下:

Where n is the number of elements in the first array and m is the number of elements in the second array. 其中n是第一个数组中的元素数, m是第二个数组中的元素数。

var data = [{ name: "Sugar" },
            { id: 1 },
            { name: "Butter" },
            { id: 2 },
            { name: "Cola" },
            { id: 3 }];
var dataNatIncluded = [{ name: "Sugar" },
                       { id: 1 },
                       { name: "Butter" },
                       { id: 2 }];

function join(a, b) {
    var hashA = {}, hashB = {}, p, result = [];
    function setter(hash) {
        return function (element) { hash[JSON.stringify(element)] = element; };
    }
    a.forEach(setter(hashA));
    b.forEach(setter(hashB));
    for (p in hashB) delete hashA[p];
    for (p in hashA) result.push(hashA[p]);
    return result;
}
// [{ name: "Cola" }, { id: 3 }]

A simple way to do that: 一种简单的方法:

var vals = [];

for(var i=0;i<data.length;i++)
{
    var found = false;
    for(var j=0;j<data_nat.length;j++)
    {
        if(data[i].id == data_nat[j].id ){
          found = true;
          break;
        }
     }
    if (!found) vals.push(data[i]);
}

JSFiddle 的jsfiddle

 for(var j=0;j<data_not_icluded.length;j++) for(var i=0;i<data.length;i++) if(data[i].id != data_not_icluded[j].id ) 

Think of what this does: For any not included object, show all objects that have not the same id as the current not included one. 想一想这有什么用:对于任何未包含的对象,请显示与当前未包含的ID具有不同ID的所有对象。 This will show many items multiple times, and it will show objects that are in "not included" but at another position. 这将多次显示许多项目,并且将显示“未包括”但在另一个位置的对象。

Instead, loop over data , check each that it is not included in data_not_included , and display it otherwise: 而是循环遍历data ,检查每个data是否不包含在data_not_included ,否则显示它:

dataloop: for (var i=0; i<data.length; i++) {
    for (var j=0; j<data_not_included.length; j++)
        if (data[i].id == data_not_icluded[j].id)
            continue dataloop;
    display(data[i]);
}

Or, using some iteration methods of Arrays : 或者,使用Arrays的一些迭代方法

data.filter(function(d) {
    return data_not_included.every(function(n) {
        return d.id != n.id;
    });
}).each(display);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM