简体   繁体   English

从本地存储javascript中的对象数组中删除对象

[英]delete an object from an array of objects in local storage javascript

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. 但是,当我调用该函数并将其参数传递给delete时,它不会删除我想要的那个,而是删除本地存储中的第一个。 Can anyone help me please? 谁能帮我吗?

You can't test 2 objects being equal by using === . 您不能使用===测试2个对象是否相等。 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) . 而是ios[i] === io ,调用isEquivalent(ios[i],io)

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 :- 这是因为您的条件永远不会得到满足,所以您的索引变量始终是未定义的 ...因此,要么在对象中传递一些唯一的键值(例如“ id”),然后比较该键值对,例如:-

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:- 使用LODASH Doc之类的第三方作为:-

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));
    }

You can use map function as, 您可以将map功能用作

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 样品

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

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