-1

I have an an array in the form:

"localValues" : [
    {
        "localValId" : "8KfbEozjdQYAefuHF",
        "localProductCode" : "291105300",
        "localMarkupVal" : "2.8",
        "localMembersPrice" : "3344"
    },
    {
        "localValId" : "qdCY6Kkvc7e8m4yxw",
        "localProductCode" : "291105300234",
        "localMarkupVal" : "2.8432",
        "localMembersPrice" : "3344333"
    },
    {
        "localValId" : "i827Eve8zBexRSTSP",
        "localProductCode" : "291105300",
        "localMarkupVal" : "2.8432",
        "localMembersPrice" : "899"
    }

I am trying to return the location of a localProductCode:

var a = localValues;

var location = a.indexOf('291105300');

console.log('location: ' + location)

However, this returns -2, which is incorrect since that code does exist in teh array. Can someone help? Thank you in advance!

Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • 1
    I think you are misunderstanding what indexOf does. The method indexOf would only work for simple values. If you had an array of localProductCode only, then indexOf would work. If you want your example to work, you would have to iterate through each of the array's subobjects and do a string comparison on the localProductCode property. Honestly though, you might be better off changing the data structure if "localProductCode" is your key. You can make local values keyed off of that instead. – Dan Smith Apr 30 '15 at 15:23
  • 1
    `indexOf` returns many things, but *never* **`-2`**! – Bergi Apr 30 '15 at 15:29

4 Answers4

1

The array doesn't contain '291105300'.

Rather than searching directly for the String, you need to find an object with that value for a given key:

function hasMatch(array, key, value) {
  var matches = array.filter(function(element) {
    return element[key] === value;
  });

  return (matches.length > 0);
}

hasMatch(localValues, 'localProductCode', '291105300') // true
joews
  • 29,767
  • 10
  • 79
  • 91
1
console.log(JSON.stringify(localValues).indexOf('291105300') != -1 ? true : false);
legui
  • 194
  • 1
  • 5
  • 1
    Apart from the horrible ` ? true : false` part, this does not reliably work for all values. It would find e.g. `'43'` even if that exsists nowhere as a property value in the array. – Bergi Apr 30 '15 at 15:31
0

The following code will find the index of the value you are looking for. This will return -1 if the value is not in the array. Taken from this SO post. I just added the alert at the end.

var myArray = [0,1,2] could become var myArray = localValues; in your case, then you can set needle to the value you are looking for.

var indexOf = function(needle) {
    if(typeof Array.prototype.indexOf === 'function') {
        indexOf = Array.prototype.indexOf;
    } else {
        indexOf = function(needle) {
            var i = -1, index = -1;

            for(i = 0; i < this.length; i++) {
                if(this[i] === needle) {
                    index = i;
                    break;
                }
            }

            return index;
        };
    }

    return indexOf.call(this, needle);
};

var myArray = localValues,
    needle = 3,
    index = indexOf.call(myArray, needle); // 1
    alert(index);

EDIT: Noticed a bit late that you may be looking for the KEY in the array.

function GetObjectKeyIndex(obj, keyToFind) {
    var i = 0, key;
    for (key in obj) {
        if (key == keyToFind) {
            return i;
        }
        i++;
    }
    return null;
}

// Now just call the following

GetObjectKeyIndex(localValues, "localProductCode");

The above snippet returns an ID if it exists, or null if no key with such a name can be found.

Community
  • 1
  • 1
Rizky Fakkel
  • 8,703
  • 1
  • 14
  • 20
  • 1
    Can you modify the example so that it works with the OPs data (instead of `[0, 1, 2]`)? Otherwise this is quite useless. – Bergi Apr 30 '15 at 15:33
0

To return an array of all the indexes where that value is found you could use map and filter:

function findValue(data, key, value) {
    return data.map(function (el, i) {
        return el[key] === value
    }).map(function (el, i) {
        return el === true ? i : null
    }).filter(function (el) {
        return el !== null;
    });
}

findValue(data.localValues, 'localProductCode', '291105300'); // [0, 2]

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95