Why jQuery.inArray doesn't work on array of objects

前端 未结 3 1392
感动是毒
感动是毒 2021-01-19 02:43

I have array of objects

var arr = [
        {\"id\" : \"1\", \"description\" : \"one\"},
        {\"id\" : \"2\", \"description\" : \"two\"},
        {\"id\"         


        
3条回答
  •  没有蜡笔的小新
    2021-01-19 03:18

    Because inArray uses === to compare elements, and different objects are never === to one another. (They're also not == to one another.)

    E.g.:

    var a = {"foo": "bar"};
    var b = {"foo": "bar"};
    console.log(a === b); // "false"
    

    You'll need to create a method on them to compare them for equivalence, and then do the search yourself.

提交回复
热议问题