如何过滤JavaScript中的json对象? [英] How to filter json object in Javascript?

查看:169
本文介绍了如何过滤JavaScript中的json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
闹钟:{
Hello:48,
World:3,
Orange:1
},
Rapid:{
Total:746084,
Fake:20970,
取消:9985,
Word:2343
},
流量:{
支持:746084,
关于:0,
学习:0
}
}

然后我加载上面的字符串并将其转换为 json object:

  jsonStr ='{Alarm:{Hello:48,World:3,Orange:1},Rapid:{Total:746084 假:20970, 取消:9985}, 流:{ 支持:746084, 关于:0, 学习:0}}; 
var jsonObj = JSON.parse(jsonStr);

现在如何过滤 json 对象按键名?例如如果过滤器是ange,过滤的对象将是:

  {
Alarm:{
Orange:1
}
}

过滤后的对象会变成:

$ $
Flow:{
支持:746084,
关于:0,
学习:0
}
}

如果过滤器是wor,结果是:

pre $ {
Alarm:{
World:3,
},
Rapid:{
Word:2343
} $




是否可以使用过滤器来实现这个过滤方法?任何建议将不胜感激。

解决方案

您可以使用 reduce() Object.keys(),它将用 indexOf()检查键名并返回所需的结果。 data-console =falsedata-console =false> div class =snippet-code>

var obj = {Alarm:{Hello:48, :3,Orange:1,Rapid:{Total:746084,Fake:20970,Cancel:9985,Word:2343,Flow 746084About:0,Learn:0}} function filterBy(val){var result = Object.keys(obj).reduce(function(r,e){if(e.toLowerCase()。indexOf ()()()()()()()() != -1){var object = {} object [k] = obj [e] [k]; r [e ] = object; }})} return r; console.log(filterBy('ange'))console.log(filterBy('flo'))console.log(filterBy('wor'))


I've got the following json string:

{
   "Alarm":{
      "Hello":48,
      "World":3,
      "Orange":1
   },
   "Rapid":{
      "Total":746084,
      "Fake":20970,
      "Cancel":9985,
      "Word": 2343
   },
   "Flow":{
      "Support":746084,
      "About":0,
      "Learn":0
   }
}

Then I load the above string and convert it to json object:

jsonStr = '{"Alarm":{"Hello":48,"World":3,"Orange":1},"Rapid":{"Total":746084,"Fake":20970,"Cancel":9985},"Flow":{"Support":746084,"About":0,"Learn":0}}';
var jsonObj = JSON.parse(jsonStr);

Now how can I filter this json object by key name? e.g. If the filter was "ange", the filtered object would be:

{
   "Alarm":{
      "Orange":1
   }
}

If the filter was "flo", the filtered object would become:

{
   "Flow":{
      "Support":746084,
      "About":0,
      "Learn":0
   }
}

And if the filter was "wor", the result would be:

{
   "Alarm":{
      "World":3,
   },
   "Rapid":{
      "Word": 2343
   }
}

Is it possible to achieve this filtering using the filter method? Any advice would be appreciated.

解决方案

You can create function using reduce() and Object.keys() that will check key names with indexOf() and return desired result.

var obj = {
  "Alarm": {
    "Hello": 48,
    "World": 3,
    "Orange": 1
  },
  "Rapid": {
    "Total": 746084,
    "Fake": 20970,
    "Cancel": 9985,
    "Word": 2343
  },
  "Flow": {
    "Support": 746084,
    "About": 0,
    "Learn": 0
  }
}

function filterBy(val) {
  var result = Object.keys(obj).reduce(function(r, e) {
    if (e.toLowerCase().indexOf(val) != -1) {
      r[e] = obj[e];
    } else {
      Object.keys(obj[e]).forEach(function(k) {
        if (k.toLowerCase().indexOf(val) != -1) {
          var object = {}
          object[k] = obj[e][k];
          r[e] = object;
        }
      })
    }
    return r;
  }, {})
  return result;
}

console.log(filterBy('ange'))
console.log(filterBy('flo'))
console.log(filterBy('wor'))

这篇关于如何过滤JavaScript中的json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆