Using jQuery inArray with array of JavaScript Objects

前端 未结 5 1264
终归单人心
终归单人心 2020-12-01 12:49

I\'m working with an array of JavaScript Objects as such:

var IssuesArray = [{\"ID\" : \"1\", \"Name\" : \"Issue1\"}, 
                   {\"ID\" : \"2\", \"         


        
相关标签:
5条回答
  • 2020-12-01 12:56

    Simplify??

    var IssuesArray = {
      1: "Issue1",
      2: "Issue2", 
      3: "Issue3"
    };
    var issue2 = IssuesArray[2];
    

    Why a list of hashes when a single hash will do?

    0 讨论(0)
  • 2020-12-01 13:04
    var spliceID = function(id, arr) {
        $(arr).each(function(i, el) {
            if (el.ID == id) {
                arr.splice(i,1);
                return false;
            }
        });
        return arr;
    }
    
    console.log(spliceID('2', IssuesArray));
    
    0 讨论(0)
  • 2020-12-01 13:11

    Not sure if I understood your question correctly, but I would do:

    $.each(IssuesArray, function(i, item){
      if (item.ID == IDToBeRemoved) IssuesArray.splice(i, 1);
    });
    
    0 讨论(0)
  • 2020-12-01 13:14

    Without using jQuery or other frameworks:

    var newArray = [];
    var i=0, len=IssuesArray.length;
    var bad_id = "2"; // or whatever
    while(i<len) {
      if(IssuesArray[i].ID !== bad_id) {
        newArray.push(IssuesArray[i++]);
      }
    }
    
    0 讨论(0)
  • 2020-12-01 13:19

    n is your list item, so something like this should do the job:

    $.grep(issuesArray, function(n) { return n.ID != "2"; })
    
    0 讨论(0)
提交回复
热议问题