1

If I have an array of objects like this:

"localValues" : [
    {
        "localValId" : "e3rQACssGkfp9zsue",
        "localProductCode" : "271102502",
        "localMembersPrice" : 7814.090000000001,
        "localProductDescription" : "11R225 146/143L H DUN SP384 FM        TL",
        "fetPr" : "29.39",
        "invPrice" : "353.85"
    },
    {
        "localValId" : "NxtmZngRpGY56grkW",
        "localProductCode" : "290132910",
        "localMembersPrice" : "300",
        "localProductDescription" : "215/70R16 99S     DUN GRNDTRK ST20 BSWTL",
        "fetPr" : "",
        "invPrice" : "136.72"
    },
    {
        "localValId" : "WXLiCMJMixndtQtqZ",
        "localProductCode" : "271102502",
        "localMembersPrice" : "444",
        "localProductDescription" : "11R225 146/143L H DUN SP384 FM        TL",
        "fetPr" : "29.39",
        "invPrice" : "353.85"
    }];

Is there a way I can check if a new localProductCode already exists in the localValues array?

Thank you.

3
  • 5
    I want to point out that is not a nested array, but an array of objects. Commented Jun 9, 2015 at 3:05
  • What do you mean by "new localProductCode"? Commented Jun 9, 2015 at 3:06
  • @user1547174, same question: stackoverflow.com/a/30720676/1074519 Commented Jun 9, 2015 at 3:10

2 Answers 2

5

you can try:

function isExisted(localValues, localProductCode) {
    for (var i = 0; i < localValues.length; ++i) {
        if (localValues[i].localProductCode == localProductCode) {
            return true;
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is a way to find the index of coincidence (like indexOf)

Array.prototype.indexOfObj = function(key, value){
    for(var i = 0; i < this.length;) 
        if(this[i++][key] === value) return --i
    return -1
}

Items.localValues.indexOfObj("localValId", "NxtmZngRpGY56grkW"); //1
Items.localValues.indexOfObj("localValId", "WXLiCMJMixndtQtqZ"); //2

Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.