0


i know that this comes a lot, but i've searched here and found no exact solution to what i'm looking for.
so, i have an array of objects that contains the following values:

 device_names = [
 {
  address: 'XX:XX:XX:XX:XX:XX',
  name: 'device name'
 },
  {
  address: 'XX:XX:XX:XX:XX:XX',
  name: 'device name'
 }]

What i want is to check whether this array contains device with a specific address, and returns true if it is there. please, don't suggest $.inArray, because it always return -1 when used with array of objects.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Kadir Damene
  • 358
  • 1
  • 3
  • 10
  • I reccomend lodash or underscore, they have great functions for stuff like this. You can of course do i in plain javascript too, you don't need jquery. I like lodashs `_.find` for something like this. For plain js - check out this thread http://stackoverflow.com/questions/8217419/how-to-determine-if-javascript-array-contains-object – ajmajmajma Apr 04 '16 at 20:45
  • 1
    its easy to write a function that iterates an array to find the first occurrence of a particular object. maybe you are hoping someone can write it for you ? – Ji_in_coding Apr 04 '16 at 20:46
  • 1
    Possible duplicate of [Find object by id in an array of JavaScript objects](http://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Shanimal Apr 04 '16 at 20:47
  • I had exactly same question - here's the answer - http://stackoverflow.com/questions/36039323/access-json-sub-levels-by-name – StudioTime Apr 04 '16 at 20:48

4 Answers4

0

Loop through the array, check if the address matches, and if so, return true.

function checkAddress(address) {
    for (var i = 0; i<device_names.length; i++) {
        if (device_names[i].address == address) {
            return true
        }
    }
}
digglemister
  • 1,477
  • 3
  • 16
  • 31
0

Can you just use $.grep instead?

Something like this... $.grep(device_names, function(d) { return d.address == "XX:XX:XX:XX:XX:XX"; })

Rob
  • 1
  • 2
0

You can create a mapping of the addresses to another array and use indexOf to see if a address is in the array.

var device_names = [
{
 address: '11:XX:XX:XX:XX:XX',
 name: 'device name'
},
 {
 address: '22:XX:XX:XX:XX:XX',
 name: 'device name'
}]

var device_addresses = device_names.map(function(obj){    
   return obj.address;
});


device_addresses.indexOf("22:XX:XX:XX:XX:XX"); // returns 1
Hoyen
  • 2,511
  • 1
  • 12
  • 13
-1

If you are prepared to use underscore.js:

function deepFind(array, needle) {
    return _.findIndex(array, function(value) {
        return _.isEqual(value, needle);
    });
}

Usage:

deepFind(device_names, {
             address: 'XX:XX:XX:XX:XX:XX',
             name: 'device name'
});

It returns the first index at which the given device is found. If there isn't one it will return -1.

if(deepFind(...) > -1)
   console.log("The device is found.");
Toonijn
  • 394
  • 2
  • 10
  • Suggesting alternative major libraries isn't usually considered helpful. – isherwood Apr 04 '16 at 20:58
  • If you're going to copy example code, please tailor it to the answer. – ajmajmajma Apr 04 '16 at 20:59
  • What do you mean with example code? The function I proposed does exactly what's needed. It finds in an array an object that equals the given needle. – Toonijn Apr 04 '16 at 21:03
  • @KadirDamene you're right including a library for such "a trivial" task is ridiculous. But it could be you already were using it. Underscore is very powerful but is quite overkill for most purposes. An object deepEquals in javascript is almost always ugly, writing it yourself isn't that reliable so a library is the way to go. – Toonijn Apr 04 '16 at 21:09