简体   繁体   English

Javascript查找数组中哪些对象具有重复的属性

[英]Javascript find which objects have duplicated properties in array

I have this array: 我有这个数组:

          "order": [
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": ["French Fries","Bacon"]
            },
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": ["French Fries","Bacon"]
            },
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": ["French Fries","Bacon"]
            },
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": ["French Fries","Bacon"]
            },
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": []
            },
            {  "item_name": " Corn Pie",
                "item_status": "ready",
                "extras": ["French Fries"]
            },
            {  "item_name": " Corn Pie",
                "item_status": "waiting",
                "extras": ["French Fries","Bacon"]
            }]

and so I need to find which objects are duplicated.. to be duplicated all the 3 properties must be the same.. I can achieve that by doing this: 所以我需要找到要复制的对象..要复制的所有3个属性必须相同..我可以通过执行以下操作来实现:

order = order.filter((item, index, self) =>
        index === self.findIndex((t) => (
            (t.item_name === item.item_name) && (t.item_status === item.item_status) && (t.extras.length === item.extras.length)
        ))
    )

this code is working like a charm.. I can understand why and how it works but I need to know which elements were filtered and how many times it did. 这段代码就像一个咒语一样工作。我可以理解它的原因和工作方式,但是我需要知道哪些元素已被过滤以及过滤了多少次。

any ideas? 有任何想法吗? thanks in advance.. I took that filter from this post post filter 在此先感谢..我从这篇帖子中删除了该过滤

from the author Eydrian, I wish I could post a comment to ask directly there but I dont have the reputation to make comments.. so here I am .. 来自作者Eydrian,我希望我可以发表评论以直接向那里提问,但是我没有发表评论的声誉..所以我在这里。

to be more clear, what i need is to know which elements where duplicated and how many times it got filtered, for example this element 更清楚地说,我需要知道哪些元素被重复以及它被过滤了多少次,例如该元素

 {  "item_name": " Corn Pie",
            "item_status": "ready",
            "extras": ["French Fries","Bacon"]
        }

is going to get filtered 4 times I need to know that information 将要过滤4次,我需要知道该信息

Here is a caveman solution (arr is your array, without the "order" key): 这是一个穴居人的解决方案(arr是您的数组,没有“ order”键):

arr.map(function(d,i){return JSON.stringify(d)})//stringfy the object
.map(function(d,i,a){//we will have multiple arrays with identical content, select the latest one
    return i === a.lastIndexOf(d) && a.filter(function(dd,ii){
        return d === dd
    })
}).filter(function(d){//filter false
    return d;
}).map(function(d,i){//map back to the same object with additional repeated key
    var retValue = JSON.parse(d[0]);
    retValue.repeated = d.length;
    return retValue;    
});

You get this: 你得到这个:

"[
    {
        "item_name": " Corn Pie",
        "item_status": "ready",
        "extras": [
            "French Fries",
            "Bacon"
        ],
        "repeated": 4
    },
    {
        "item_name": " Corn Pie",
        "item_status": "ready",
        "extras": [],
        "repeated": 1
    },
    {
        "item_name": " Corn Pie",
        "item_status": "ready",
        "extras": [
            "French Fries"
        ],
        "repeated": 1
    },
    {
        "item_name": " Corn Pie",
        "item_status": "waiting",
        "extras": [
            "French Fries",
            "Bacon"
        ],
        "repeated": 1
    }
]"

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

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