javascript - 使用 Underscorejs 从 json 中提取值

标签 javascript arrays json underscore.js

我有一个 json,需要从中过滤 name 值与传递的输入匹配的对象,然后从相应的 links< 中提取 href 对象的 ref 值为 self。我附上代码片段以及我的过滤方法。我正在使用 Underscore.js

var jsonObj = [
{
  "links": [
    {
      "rel": "self",
      "href": "http://hostname:port/state/644444",
      "variables": [],
      "variableNames": [],
      "templated": false
    },
    {
      "rel": "city",
      "href": "http://hostname:port/city/6184",
      "variables": [],
      "variableNames": [],
      "templated": false
    }
  ],
  "stateId": 65986,
  "countyId": 6184,
  "name": "AAATest",
  "population": 20000,
  "location": "SFS CSR"
},
{
  "links": [
    {
      "rel": "self",
      "href": "http://hostname:port/state/65987",
      "variables": [],
      "variableNames": [],
      "templated": false
    },
    {
      "rel": "city",
      "href": "http://hostname:port/city/6184",
      "variables": [],
      "variableNames": [],
      "templated": false
    }
  ],
  "stateId": 65987,
  "countyId": 6184,
  "name": "2k8std511",
  "population": 20000,
  "location": "SFS CSR"
  }
]

var keywords='2k8std511';

var filtered = _.filter(jsonObj, function (item) {
                return (_.contains('2k8std511', item['name']));
            });
console.log(_.chain(jsonObj)
 .pluck("links")
 .flatten()
 .value());

请告诉我如何根据给定关键字进行过滤:

示例输入:AAATest 预期输出:[href:'http://hostname:port/state/644444 ']

问候, 普拉迪普

最佳答案

下划线解决方案:

var jsonObj = [{
  "links": [{
    "rel": "self",
    "href": "http://hostname:port/state/644444",
    "variables": [],
    "variableNames": [],
    "templated": false
  }, {
    "rel": "city",
    "href": "http://hostname:port/city/6184",
    "variables": [],
    "variableNames": [],
    "templated": false
  }],
  "stateId": 65986,
  "countyId": 6184,
  "name": "AAATest",
  "population": 20000,
  "location": "SFS CSR"
}, {
  "links": [{
    "rel": "self",
    "href": "http://hostname:port/state/65987",
    "variables": [],
    "variableNames": [],
    "templated": false
  }, {
    "rel": "city",
    "href": "http://hostname:port/city/6184",
    "variables": [],
    "variableNames": [],
    "templated": false
  }],
  "stateId": 65987,
  "countyId": 6184,
  "name": "2k8std511",
  "population": 20000,
  "location": "SFS CSR"
}]

var filtername = 'AAATest';
var answer = _.chain(jsonObj)
  .filter(function(obj) {
    return obj.name === filtername;
  })
  .map(function(obj) {
    return obj.links
  })
  .first()
  .filter(function(obj) {
    return obj.rel === 'self'
  })
  .map(function(obj) {
    return obj.href
  })
  .value();
console.log(answer);
<script src="http://underscorejs.org/underscore-min.js"></script>

关于javascript - 使用 Underscorejs 从 json 中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163514/

相关文章:

ios - 如何以特定顺序显示数组中的项目?

php - Math & php : FAST sort of an array [1. .N] 以一种特殊的方式

mysql - 通过 MySQL 中的 bool 值查找 JSON 对象路径

c# - 使用JSON(UWP,C#)获取YouTube统计信息

javascript - 在 Firefox 上加载页面之前和之后执行 GM_xmlhttpRequest

javascript - 导出数组以在另一个 javascript 文件中使用

javascript - 改变JavaScript中对象的结构

php - 将其他对象添加到 JSON 编码数组

javascript - 折叠函数参数的最佳方式是什么?

javascript - 无法点击网页中的任何链接