从json对象解析特定记录

时间:2013-04-21 08:32:34

标签: javascript json

我有以下json对象

       var testObj = {
        "CompanyA": [
            { "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 172122, "2020": 103716 } },
            { "geography": [ "Europe", "France" ], "productLine": "Clothing", "revenue": { "2022": 85693, "2021": 91790, "2020": 77650 } },
            { "geography": [ "Europe", "France" ], "productLine": "Electronics", "revenue": { "2022": 121987, "2021": 62435, "2020": 65834 } },
            { "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 107447, "2020": 145543 } },
            { "geography": [ "Europe", "Germany" ], "productLine": "Clothing", "revenue": { "2022": 77903, "2021": 97139, "2020": 110346 } },
            { "geography": [ "Europe", "Germany" ], "productLine": "Electronics", "revenue": { "2022": 110897, "2021": 155282, "2020": 128696 } },
            { "geography": [ "South America", "Brazil" ], "productLine": "Clothing", "revenue": { "2022": 66217, "2021": 55798, "2020": 66643 } },
            { "geography": [ "South America", "Brazil" ], "productLine": "Electronics", "revenue": { "2022": 94262, "2021": 100560, "2020": 56272 } }
          ],
          "CompanyB": [
            { "geography": [ "Europe", "United Kingdom" ], "productLine": "Produce", "revenue": { "2022": 281110, "2021": 242965, "2020": 221863 } },
            { "geography": [ "Europe", "United Kingdom" ], "productLine": "Clothing", "revenue": { "2022": 168270, "2021": 121161, "2020": 60919 } },
            { "geography": [ "Europe", "United Kingdom" ], "productLine": "Electronics", "revenue": { "2022": 239537, "2021": 131959, "2020": 97047 } },
            { "geography": [ "Europe", "Ireland" ], "productLine": "Produce", "revenue": { "2022": 74963, "2021": 43406, "2020": 54623 } },
            { "geography": [ "Europe", "Ireland" ], "productLine": "Clothing", "revenue": { "2022": 44872, "2021": 24797, "2020": 16010 } },
            { "geography": [ "Europe", "Ireland" ], "productLine": "Electronics", "revenue": { "2022": 63877, "2021": 94185, "2020": 87098 } }
          ],
         .........
       };

我的页面中有三个下拉菜单,如公司,地理位置(CountryName2nd元素)和ProductLine ......

根据所选的下拉值,我需要获得收入..

我能够捕获用户选择的公司名称地理位置(countryName) productLine ,但却很难获得该特定收入。 ..

我可以获得这样的收入

console.log(testObj[company][1].revenue);

但在这里我不确定特定记录,因为我需要根据country和productLine进行选择

我得到了解决方案。但我想知道什么是解析json对象的最佳方法...是根据我们的需要重建还是循环遍历json对象......

2 个答案:

答案 0 :(得分:0)

你只需循环遍历数组,直到找到匹配的数组:

var companyArr = testObj[company];
for(var i = 0; i < companyArr.length; i++){
    if(companyArr[i].geography[1] == country && companyArr[i].productLine == productLine)
        break;
}

if(i < companyArr.length){ // Found a match
    console.log(companyArr[i]); // Log the matching object
}

请注意,如果重构json是一个选项,那么你可以完全避免循环,只需执行以下操作:

testObj[company][country][productLine].revenue

答案 1 :(得分:0)

您可以像这样过滤匹配的对象

var company = "CompanyA";
var country = "Germany";
var productLine = "Produce";

var res = testObj[company].filter(function(el){
  return el.geography[1] == country && productLine == el.productLine;
});
console.log(res);