0

I have an array of objects in local storage like this:

ios = [{
    "id" : 1,
    "type" : "in",
    "cat" : "Diversi",
    "qty" : 50,
    "date" : "2016-11-02T09:51:48.872Z",
    "descr" : ""
  }, {
    "id" : 1,
    "type" : "in",
    "cat" : "Lavoro",
    "qty" : 50,
    "date" : "2016-11-02T10:08:11.362Z",
    "descr" : ""
  }];

I want to delete one of the objects with this function:

function remove(io) {
    var ios = localStorage.getItem('ios') ? JSON.parse(localStorage.getItem('ios')) : [];
    var index;
    for (var i = 0; i < ios.length; i++) {
        if (ios[i] === io) {
          index=i;
          break;
        }
    }
    ios.splice(index, 1);
    localStorage.setItem('ios', JSON.stringify(ios));
}

But when i call the function, and pass it the parameter to delete, it doesn't delete the one I want, but the first one in local storage instead. Can anyone help me please?

4
  • 1
    try replacing if (ios[i] === io) { with if (JSON.stringify(ios[i]) === JSON.stringify(io)) { Commented Nov 2, 2016 at 10:43
  • what gets passed to remove? Is it an object? Commented Nov 2, 2016 at 10:46
  • @OliverRadini yes i pass an object Commented Nov 2, 2016 at 10:51
  • Don't splice, filter. Commented Nov 2, 2016 at 11:02

3 Answers 3

1

This is because your condition never get satisfied, so your index variable is always undefined ... So either pass some unique key value in the object like "id" and compare that key value pair like :-

function remove(id) {
    var ios = localStorage.getItem('ios') ? JSON.parse(localStorage.getItem('ios')) : [];
    var index;
    for (var i = 0; i < ios.length; i++) {
        if (ios[i].id === id) {
          index=i;
          break;
        }
    }
    if(index === undefined) return 
    ios.splice(index, 1);
    localStorage.setItem('ios', JSON.stringify(ios));
}

OR

use some third party like LODASH Doc as:-

function remove(io) {
        var ios = localStorage.getItem('ios') ? JSON.parse(localStorage.getItem('ios')) : [];
        var index;
        index = _.findIndex(ios , io);
        if(index === -1) return 
        ios.splice(index, 1);
        localStorage.setItem('ios', JSON.stringify(ios));
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You can't test 2 objects being equal by using ===. Try to use this function:

function isEquivalent(a, b) {
    var aProps = Object.getOwnPropertyNames(a);
    var bProps = Object.getOwnPropertyNames(b);

    if (aProps.length != bProps.length) {
        return false;
    }

    for (var i = 0; i < aProps.length; i++) {
        var propName = aProps[i];

        if (a[propName] !== b[propName]) {
            return false;
        }
    }

    return true;
}

And instead ios[i] === io, you call isEquivalent(ios[i],io).

Comments

0

You can use map function as,

function removeItem() {    
  var ios = JSON.parse(localStorage.getItem('ios'));
  var index = ios.map(function(element) {
    return element.cat;
  }).indexOf(io.cat);
  ios.splice(index, 1);
  localStorage.setItem('ios', JSON.stringify(ios));
}

to remove item based on passed object's cat in the array. Sample

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.