admin管理员组

文章数量:1417405

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.

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.

Share Improve this question edited Apr 20, 2021 at 11:46 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Jun 9, 2015 at 3:02 Trung TranTrung Tran 13.8k48 gold badges126 silver badges209 bronze badges 3
  • 5 I want to point out that is not a nested array, but an array of objects. – Daemedeor Commented Jun 9, 2015 at 3:05
  • What do you mean by "new localProductCode"? – Phil Commented Jun 9, 2015 at 3:06
  • @user1547174, same question: stackoverflow./a/30720676/1074519 – Walter Chapilliquen - wZVanG Commented Jun 9, 2015 at 3:10
Add a ment  | 

2 Answers 2

Reset to default 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;
}

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

本文标签: javascriptCheck if value exists in array of objectsStack Overflow