HippoDuck
HippoDuck

Reputation: 2194

How to search a multidimensional array for value?

I have an array which I would like to search. I found a method using $,map but I can't get this working.

My array is generated like this: (Using a PHP loop which gets the data from MySQL)

clientList.push = [{'ID' : '1', 'FullName' : 'Company1'}]
clientList.push = [{'ID' : '2', 'FullName' : 'Company2'}]
clientList.push = [{'ID' : '3', 'FullName' : 'Company3'}]

I am trying to use the following to return the "FullName" value Where ID = 2. This is an example from another question.

var found = $.map(clientList, function(item) {
    if (item.ID.indexOf('2') >= 0) {
        return item;
    }
});

if (found.length > 0) {
    alert(found[0].FullName);
}

However this doesn't return anything, and I don't get any Javascript errors.

What am I doing wrong?

Upvotes: 0

Views: 6234

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074276

This is incorrect:

clientList.push = [{'ID' : '1', 'FullName' : 'Company1'}]
clientList.push = [{'ID' : '2', 'FullName' : 'Company2'}]
clientList.push = [{'ID' : '3', 'FullName' : 'Company3'}]

You're assigning arrays to the push property, which is a method of the array.

If you have an array referenced from clientList, then to add to it you call push (no =, and note the ( ... );):

clientList.push([{'ID' : '1', 'FullName' : 'Company1'}]);
clientList.push([{'ID' : '2', 'FullName' : 'Company2'}]);
clientList.push([{'ID' : '3', 'FullName' : 'Company3'}]);

Separately, that $.map code won't work, the entries in clientList don't have an ID property (they're arrays; the objects inside them have ID properties).

You've said in a comment:

My aim was to dump a MySQL table into a Jquery array which can be searched

Then you don't want an array of arrays of objects, just an array of objects:

var clientList = []
// Presumably simulating the results of an ajax query
clientList.push({'ID' : '1', 'FullName' : 'Company1'});
clientList.push({'ID' : '2', 'FullName' : 'Company2'});
clientList.push({'ID' : '3', 'FullName' : 'Company3'});

To find the entry int here with ID == 2, you'd use Array#find (new in ES2015, but shimmable/polyfillable):

var item = clientList.find(function(item) {
    return item.ID == "2";
});

Live example:

var clientList = []
// Presumably simulating the results of an ajax query
clientList.push({'ID': '1', 'FullName': 'Company1'});
clientList.push({'ID': '2', 'FullName': 'Company2'});
clientList.push({'ID': '3', 'FullName': 'Company3'});

var item = clientList.find(function(item) {
  return item.ID == "2";
});
console.log(item);

Upvotes: 6

RonyLoud
RonyLoud

Reputation: 2426

The push() method adds new items to end of an array. Use this fiddle:

var clientList=[];
clientList.push({'ID' : '1', 'FullName' : 'Company1'})
clientList.push({'ID' : '2', 'FullName' : 'Company2'})
clientList.push({'ID' : '3', 'FullName' : 'Company3'})

var found = $.map(clientList, function(item,index) {
    if (item.ID == 2) {
        return item;
    }
});

if (found.length > 0) {
    alert(found[0].FullName);
}

Upvotes: 1

Related Questions