0

I want to find the same object in an array and remove it from the array. Here is an example.

var previousSelectedWords = [{ 'id': 33,  'name': "美白", 'kana': "ビハク", 'type': 'new'}, { 'id': 3, 'name': "テスト", 'kana': "テスト", 'type': 'new'}, { 'id': 34,  'name': "免疫力", 'kana': "メンエキリョク", 'type': 'old'}];

var checkCurrentWord = { 'id': 3, 'name': "テスト", 'kana': "テスト", 'type': 'new'};

var foundWordIndex = $.inArray(checkCurrentWord, previousSelectedWords)

Looks like $.inArray doesn't work with objects. It always gives me -1

id and name, and kana can be the same, only type is unique.

whitesiroi
  • 2,725
  • 4
  • 30
  • 64
  • Two objects are never equal, so the correct result is in fact `-1`, always. – adeneo Dec 24 '15 at 11:51
  • @adeneo Thank you for your comment. I did read that post, but wasn't able to find the answer. "carBrands.indexOf(car1);" doesn't work – whitesiroi Dec 24 '15 at 12:01
  • 2
    No, `array.indexOf(object)` does not work, you can't compare two objects, because they are ***never*** the same. You'd have to loop over all the keys and values in the object, and check each one, and see if they all match, and still the objects aren't *"the same"*, but they do have the same keys and values, which is probably what you want to know. – adeneo Dec 24 '15 at 12:06

0 Answers0