从json对象解析一个特定的记录
parse a particular record from json object
我有以下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 } }
],
.........
};
在我的页面中有三个下拉菜单:Company, Geography(CountryName2nd元素)and ProductLine…
根据选择的下拉值,我需要选择收入…
我能够捕获用户选择的公司名称, 地理(countryName)和productLine,但难以获得特定的收入…
我可以得到这样的收入
console.log(testObj[company][1].revenue);
但是这里我不确定具体的记录,因为我需要根据国家和产品线进行选择
我得到了这个问题的解决方案。但我想知道什么是解析json对象的最佳方式…
您可以像这样筛选匹配对象
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);
你只需要循环遍历数组,直到找到匹配的数组:
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
相关文章:
- 将一个表的id与另一个表中的user_id进行匹配,以获取mysql中的记录
- 我怎么能把一个错误当作未捕获的错误来记录呢
- HTML5历史记录-返回上一个完整页面按钮
- 如何记录调用另一个函数的函数的返回值
- 制作一个阻止存储在浏览器历史记录中的网页
- 在Javascript中的一个Open()记录集函数中使用多个查询
- 在ember.js记录上使用toJSON,将其中一个属性设置为null
- $http请求在一个请求中返回所有记录而不分页
- ASP ADO记录集.NextRecordset()在没有下一个记录集时中断
- 为什么这个JS for在循环中只记录一个属性的键,而不是整个属性
- Rails,javascript在转到下一个记录时不起作用
- 将两个数组连接到一个记录数组中
- 从AngularJS在Rails中创建一个记录
- Emberjs将select's的值设置为一个记录
- 我如何找到一个记录烬与关联承诺
- PHP或javascript文本文件分号db使用一个记录显示另一个悬停
- Extjs 4.2可排序的网格与固定位置的一个记录
- Meteor JS:我如何访问集合中的前一个记录
- 我必须在客户端创建一个记录吗?
- 如何使只有一个记录在网格作为不可编辑使用Kendoui