繁体   English   中英

数数 具有多个键中特定值的json对象中的条目数

[英]Count no. of entries in a json object having specific values in multiple keys

有没有一种方法可以使用javascript从json对象获取/搜索数据,类似于触发查询?

我的情况是:我有一个大的json对象(如果您将其视为数据库,则它有12列和1000行)。 我需要知道有多少行具有特定的值对(即在第1列中具有,“ USA”和在第2列中具有“ INDIA”的行数)。

例如(例如,我创建了一个小示例json对象)

json对象:

{"export":
   [{"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"},    
    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    
    {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"},    
    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    
]}

在该示例中,由于存在两行,因此from是USA,to是INDIA。

可以认为类似于mysql查询:

select * from export where from = "USA" and to = "INDIA";

我可以使用javascript在json对象上执行此操作吗?

如果没有,是否可以从csv文件中执行类似的操作,我是否也拥有该数据的csv文件?

是的,只需过滤数组以获取满足查询条件的元素,然后计算剩余的长度即可。 这是可重复使用的功能形式的代码

function counter(f, t) {
  return json.export.filter(function(elem) {
    return elem.from===f && elem.to===t;
  }).length;
}

您可以使用filter()length属性。 filter()将根据我们的条件过滤数组,要获取数组计数,请使用length属性。

 var data = { "export": [{ "goods": "Wheat ", "from": "GHANA", "to": "AUSTRALIA" }, { "goods": "Wheat", "from": "USA", "to": "INDIA" }, { "goods": "Wheat", "from": "SINGAPORE", "to": "MALAYSIA" }, { "goods": "Wheat", "from": "USA", "to": "INDIA" }, ] }; var count = data.export.filter(function( v) { return v.from == 'USA' && v.to == 'INDIA' }).length; alert(count); 

您可以使用过滤器来编码匹配多个条件的表达式:

o.export.filter(function(a){return a.from=="USA" && a.to=="INDIA"}).length

我喜欢可重用的过滤器回调,它可以让您在调用时而不是代码时更改条件:

function match(x){ return x[this[0]]===this[1]; }
//usage:
o.export.filter(match,["from", "USA"]).filter(match, ["to","INDIA"]).length;

可重复使用的函数使您可以设置不同的列并比较值,而无需重写函数。

这应该工作:

var a = {"export":
   [{"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"},    
    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    
    {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"},    
    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    
]};

var b = 0;

for(var i = 0; i < a.export.length; i++){
  if( a.export[i].from == 'USA' && a.export[i].to == 'INDIA'){
    b++;
  }
}

这是结构化数据的结构化查询的更详细示例,并带有对结果数组进行排序的好处。

它具有一个带有三个参数的小功能select(from, where, sort)

  • from :数据源作为数组,是必需的。

  • 其中 :具有一个或多个键/值对的对象,如果提供多个​​,则条件与链接。 如果为假,则忽略该参数。

  • sort :具有键/值对对象的数组,该对象对键值从1升至值-1降序对结果集进行排序 排序具有字符串排序功能。 如果为假,则忽略该参数。

测试用例返回:

  1. from = 'USA'to = 'INDIA'的所有集合的计数。
  2. 所有带有from = 'USA'to = 'INDIA'集合,未排序。
  3. 所有组排序from上升。

 var data = { "export": [ { "goods": "Wheat ", "from": "GHANA", "to": "AUSTRALIA" }, { "goods": "Wheat", "from": "USA", "to": "INDIA" }, { "goods": "Wheat", "from": "SINGAPORE", "to": "MALAYSIA" }, { "goods": "Wheat", "from": "USA", "to": "INDIA" } ] }; function select(from, where, sort) { var data = from.slice(0); where && Object.keys(where).forEach(function (k) { data = data.filter(function (d) { return d[k] === where[k]; }); }); sort && data.sort(function (a, b) { var value = 0; sort.some(function (el) { var key = Object.keys(el)[0]; value = ~el[key] ? a[key].localeCompare(b[key]) : b[key].localeCompare(a[key]); return value; }); return value; }); return data; } function print(o) { document.body.innerHTML += '<pre>' + JSON.stringify(o, 0, 4) + '</pre>'; } print(select(data.export, { from: 'USA', to: 'INDIA' }).length); print(select(data.export, { from: 'USA', to: 'INDIA' })); print(select(data.export, {}, [{ from: 1 }])); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM