1

I got stuck trying to retrive array items. So here is the deal. I have a two dimentional array which has value and key so example of my data is:

[
    Object { css="SS", title="Apple"}, 
    Object { css="SS", title="Orange"}, 
    Object { css="SS", title="Banana"}
]

I want to see if an object exists in the array above. And I have no idea why its not working, here is my code to find the object:

jQuery.inArray("Apple", fruits["title"]); //this returns -1 why?

Any ideas how to search two dimensional array?

1
  • What is fruits - does that contain the array of objects? Commented Sep 19, 2013 at 14:18

7 Answers 7

1

This is not a 2D array, this is an array of objects, so this should work:

for (var i = 0; i < array.length; i++) {
    console.log(array[i].title); //Log the title of each object.
    if (array[i].title == "Apple") {
        console.log("Found apple!");
    }
}

Also, objects are key/val pairs, denoted by key : val, not key = val. Your array has syntax errors and shouldn't run.

Sign up to request clarification or add additional context in comments.

Comments

1

To be pedantic, you have an array of objects, not a 2d array. Also your syntax for the object parameters is incorrect.

You can use filter() on the array to find the values:

var array = [
    { css: "SS", title: "Apple"}, 
    { css: "SS", title: "Orange"}, 
    { css: "SS", title: "Banana"}
];
var matches = array.filter(function (obj) { return obj.title == "Apple" });
if (matches.length) {
    // Apple was in the array...
}

2 Comments

So, you say I should call obj.title to see what is in that array under value "title", but when I console.log(obj.title); it returns undefiened D; (when I do console.log(obj[0].title); it returns first item, can I return all rather than one by one) I cant figure out why.. when I console.log(obj); it returns as I showed in example data.
@JavaNoob for( var i = 0; i < obj.length; i++ ) { console.log( obj[i].title ); }
0

If you have an object like this

var peoples = [
  { "name": "bob", "dinner": "pizza" },
  { "name": "john", "dinner": "sushi" },
  { "name": "larry", "dinner": "hummus" }
];

Ignore what's below. Use the filter method!

peoples.filter(function (person) { return person.dinner == "sushi" });
  // => [{ "name": "john", "dinner": "sushi" }]

You can search for people who have "dinner": "sushi" using a map

peoples.map(function (person) {
  if (person.dinner == "sushi") {
    return person
  } else {
    return null
  }
}); // => [null, { "name": "john", "dinner": "sushi" }, null]

or a reduce

peoples.reduce(function (sushiPeople, person) {
  if (person.dinner == "sushi") {
    return sushiPeople.concat(person);
  } else {
    return sushiPeople
  }
}, []); // => [{ "name": "john", "dinner": "sushi" }]

I'm sure you are able to generalize this to arbitrary keys and values!

Comments

0

fruits probably is a array, fruits["title"] therefor doesn't exist. You might want to transform your data:

var fruitTitles = fruits.map(function(f) { return f.title; });
jQuery.inArray("Apple", fruitTitles);

Comments

0

From the jQuery docs: jQuery.inArray( value, array [, fromIndex ] )

I've never used this method, but a quick guess: var hasIt = jQuery.inArray({css:"SS",title:"Apple"}, myArray);

1 Comment

That wouldn't work for the same reason that {css:"SS",title:"Apple"}!={css:"SS",title:"Apple"} evaluates as true - you create a new object in your first parameter to .inArray() and the array doesn't contain that object, it just contains a similar object that has properties with the same values.
0

As the $.inArray() documentation explains, the first argument to the function is the value to search for. Your array does not have any elements that are equal to the string "Apple" that you have supplied in the first argument because none of your array elements are strings (they're all objects).

The second argument to $.inArray() is supposed to be the array itself, but (assuming fruits is the array you show) fruits["title"] is undefined because your array has no property called "title", only the objects in the array have that property.

Try this instead:

var index = $.inArray("Apple", $.map(fruits, function(el) { return el.title; }));

Comments

0

try this code

var fruits = [
    { css:"SS", title:"Apple"}, 
    { css:"SS", title:"Orange"}, 
    { css:"SS", title:"Banana"}
];

jQuery.grep(fruits,function(fruit){return fruit.title == 'Apple'});

Comments

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.