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

3
  • 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. Commented May 21, 2015 at 9: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 Commented May 21, 2015 at 9:27
  • I think you might find a possible answer here : [array.contains(obj) in JavaScript][1] ! [1]: stackoverflow.com/questions/237104/… Commented May 21, 2015 at 9:30

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

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

Comments

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

1 Comment

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" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.