JSB
JSB

Reputation: 231

Trying to sort an array of objects but splice is not a function?

The global picture is that i am trying to remove duplicates from an array of objects. Objects with the same advertId and leadboxId are considerd duplicates but for testing purposes i am only checking advertIds

I am getting this array from the sessionStorage and removing duplicates.

var testSort = function () {
    var events = [];
    events = sessionStorage.events;
    console.log("events unsorted");
    console.log(events);
    for (var i = 0; i < events.length; i++) {
        for (var x = i + 1; x < events.length; x++) {
            if (events[x].advertId == events[i].advertId) {
                events.splice(x, 1);
                --x;
            }
        }
        // add
    }

The console prints out the events array as such:

[{"module":"slick_module","eventType":"swipe","leadboxId":"1565","advertId":"5653","length":"length of event","time":1462783354789,"posted":"postedStatus"},{"module":"slick_module","eventType":"swipe","leadboxId":"1565","advertId":"56527","length":"length of event","time":1462783357590,"posted":"postedStatus"}]

Is this not a good array? When trying to splice this i get the error that events.splice is not a function.

Any help is aprecciated.

Upvotes: 0

Views: 111

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You can't store an array/object in SessionStorage.
Web storage can only store strings.
You have a string in the sessionStorage['events'] key, processed via JSON.stringify() method.
To deal with array for further filtering - decode that string with JSON.parse() method like:

var arr = JSON.parse(sessionStorage['events']);
...

https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API

Upvotes: 1

Redu
Redu

Reputation: 26191

As always Array.prototype.reduce() to help with a single liner

var events = [{"module":"slick_module","eventType":"swipe","leadboxId":"1565","advertId":"5653","length":"length of event","time":1462783354789,"posted":"postedStatus"},{"module":"slick_module","eventType":"swipe","leadboxId":"1565","advertId":"56527","length":"length of event","time":1462783357590,"posted":"postedStatus"}],
  filtered = events.reduce((p,c) => !~p.findIndex(e => e.advertId == c.advertId) ? p.concat(c) : p, []);
document.write("<pre>" +  JSON.stringify(filtered, null,2) + "</pre>");

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

You could use Array#filter with a hash table for already inserted elements.

var array = [{ "module": "slick_module", "eventType": "swipe", "leadboxId": "1565", "advertId": "5653", "length": "length of event", "time": 1462783354789, "posted": "postedStatus" }, { "module": "slick_module", "eventType": "swipe", "leadboxId": "1565", "advertId": "56527", "length": "length of event", "time": 1462783357590, "posted": "postedStatus" }, { "module": "slick_module", "eventType": "swipe", "leadboxId": "1565", "advertId": "56527", "length": "length of event", "time": 1462783357590, "posted": "postedStatus" }],
    filtered = array.filter(function (a) {
        var key = a.leadboxId + '|' + a.advertId;
        if (!this[key]) {
            this[key] = true;
            return true;
        }
    }, Object.create(null));

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');

Upvotes: 1

Related Questions