5

I need to check if an object is logically in an array, when two object are logically equals(Just like the equals in Java), it will be treated as "in" the array

While I use $.inArray of jQuery to test below code, it retuans -1,indicating that the copied one is not treated as "in" the array.

var a =[{value: "G27", title: "G27"}];
$.inArray({value: "G27", title: "G27"},a); //returns -1

Above is just an example ,Is there an easy way for generic cases to achieve that

JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • 2
    In javascript no two obects are ever the same, so it's going to be generally difficult, and you should probably try to figure out a better way of structuring your data. – adeneo May 21 '15 at 09:22
  • You are creating two objects, but when you do it like this, it will work: `var b = {value: "G27", title: "G27"}; var a =[b]; $.inArray(b,a); //returns 0` – Martin Adámek May 21 '15 at 09:27
  • I think you might find a possible answer here : [array.contains(obj) in JavaScript][1] ! [1]: http://stackoverflow.com/questions/237104/array-containsobj-in-javascript – KevinD May 21 '15 at 09:30

4 Answers4

1

A workaround would be to check for each key-value pair in a for loop:

function exist(arr, obj){
  var len = Object.keys(obj).length;
  var count = 0;
  for(var i=0;i<arr.length;i++){
      count=0;
      for(var key in obj){
          if(obj[key] == arr[i][key]){
            count++;
          }
      }
      if(count == len && count == Object.keys(arr[i]).length){
        console.log("Exists!!");
        return;
      }
  }
  console.log("Don't exist!!");
}

var arr =[{value: "G27", title: "G27"}];
var b = {value: "G27", title: "G27"};
//Call
exist(arr, b);
Zee
  • 8,420
  • 5
  • 36
  • 58
  • This is a non dynamic solution, because it assumes a simple object with only Key/value attributes. So, each time a new property is added to the object, he has to edit this loop – Muhammad Gouda May 21 '15 at 09:35
1

If you feel like using underscore.js, you could try using _.findWhere():

var a = [{value: "G27", title: "G27"}];
snippet.log(_.findWhere(a, {value: "G27", title: "G27"}) !== undefined); // returns true
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • Hi @Scimonster, no problem to reject my edit .. as I see you made the answer even better, I just liked your answer and tried to proof how it works :) – Muhammad Gouda May 21 '15 at 10:42
0

You could use the filter method of the array.

var arr = [{value: "G27", title: "G27"}];
// Don't call filter on arr because this will mutate arr itself. Instead of creating a new array.
var filteredArr = Array.prototype.filter.call(arr, function(item) {
    return item.value == "G27" && iem.title == "G27";
});

if(filteredArr.length > 0) {
    // Item exists
}
Dibran
  • 1,435
  • 16
  • 24
0

You may try custom search:

var indexOf = function(array, obj) {
  var i = array.length;
  var o;
  while (o = array[--i]) //note, its assignment
    if (obj.value === o.value && obj.title === o.title)
      break;
  return i;
};

var arr = [{
  value: "G27",
  title: "G27"
}, {
  value: "G28",
  title: "G28"
}, {
  value: "G29",
  title: "G29"
}];

console.log(indexOf(arr, {
  title: "G28",
  value: "G28"
})); //<-- 1

console.log(indexOf(arr, {
  title: "G30",
  value: "G30"
})); //<-- -1
  • Problem is for object defined e.g as: `{ title: "G28", value: "G28" }` Or `{ title: "G28", value: "G28" }` is the same than `{ value: "G28", title: "G28" }` – A. Wolff May 21 '15 at 10:53