0

I want to find duplicates of objects in an array. I wanna do this by sorting the array and then comparing the items with each other. How can I compare an object "as a string" that means if there is any difference at all they get sorted accordingly?

It's the sort-function I would like to replace.

    var newCart = shoppingCart.sort();

    var results = [];
    for (var i = 0; i < newCart.length - 1; i++) {
        if (newCart[i + 1] == newCart[i]) {
            results.push(newCart[i]);
        }
    }

    this.products = results;

Example input:

[
    {
        "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
    },

    {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },

    {
        "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
    },

    {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29100,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },
    {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },
]

Example output:

[
    {
        "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
    },
    {
        "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
    },

    {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },

    {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },
        {
        "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29100,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
    },
]

Note that the last item in the output ends up last, as the price is not the same as the other items with the same ID.

Himmators
  • 14,278
  • 36
  • 132
  • 223

2 Answers2

0

You can use JSON.stringify

    function sort(arr) {
        if (arr.length == 0) return [];
        var result = [];
        var v = arr[0];
        result.push(v);
        arr.splice(0,1);;
        for (var i = 0; i < arr.length - 1; i++) {
            if (JSON.stringify(arr[i]) == JSON.stringify(v)) {
                result.push(arr[i]);
                arr.splice(i,1);
            }
        }
            return result.concat(sort(arr));
    }

console.log(JSON.stringify(sort(arr)));

DEMO

vikrant singh
  • 2,091
  • 1
  • 12
  • 16
0

I have done this simple JavaScript sort method:

arr.sort(function(a,b) { return parseFloat(a.price) - parseFloat(b.price) } );

Solution

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Suresh Mahawar
  • 1,458
  • 15
  • 34