计算特定的json /单词出现多少次

时间:2018-07-06 14:56:45

标签: json node.js

如果我的网站包含带有多个"usrId":的JSON,我该如何计算它存在多少次?我不知道计数JSON对象还是只计算该单词在网页上存在的次数是更好的方法,我也不知道如何开始,该怎么做?

request(getusr, function(err, res, body) {
  let json = JSON.parse(body);
  let usramount = // count how many times usrId appears
})

示例json:

  {  
     "id":"example",
     "usrId":"example"
  },
  {  
     "id":"example"
     "usrId":"example",
  },

1 个答案:

答案 0 :(得分:2)

(body.match(/"usrId":/g) || []).length;

应该可以解决问题。

相关问题