-1

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:

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 ..

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

Miguel Sedek
  • 173
  • 2
  • 11
  • What are expected results? Not very clear what you are asking – charlietfl Apr 22 '18 at 22:56
  • the result should be an array that contains non duped elements, the dupe "status" is given bu having all the properties the same, for example, havin same name, same status and same length in the extras... I can do that already with the filter but i need know which elements where filtered and how many times it was filtered – Miguel Sedek Apr 22 '18 at 23:01
  • 1
    Right...you said all that. What are expected results for the dups info and how would it be used? – charlietfl Apr 22 '18 at 23:03
  • 2
    In order to achieve that, your best bet would be to re-write the JS so it doesn't use one-line arrow functions. Give yourself a bit more room to perform some other logic – Denno Apr 22 '18 at 23:10
  • 1
    You could run the same filter and just invert it – Lux Apr 22 '18 at 23:27

1 Answers1

2

Here is a caveman solution (arr is your array, without the "order" key):

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
    }
]"
ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22
  • thanks man this solves my problem, im kind of new at javascript, dont know whats the thing with the downvoting.. I think that I was pretty clear with my problem and provided enough information... guess that trolls gonna hate everywhere all the time – Miguel Sedek Apr 23 '18 at 00:04
  • I understand you man. I'm glad you got what you wanted. Good luck with your work. – ibrahim tanyalcin Apr 23 '18 at 00:33