首页 > 解决方案 > 计算一个特定的 json/word 出现了多少次

问题描述

如果我有一个包含多个 JSON 的网站,"usrId":我如何计算它存在多少次?我不知道是计算 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",
  },

标签: jsonnode.js

解决方案


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

应该做的伎俩。


推荐阅读