使用Underscorejs从json中提取值

时间:2015-01-27 04:56:55

标签: javascript arrays json underscore.js

我有一个json,我需要从中过滤name值与传递的输入匹配的对象,然后从具有{{1}的相应href对象中获取links值} 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());  预期输出:[href:' http://hostname:port/state/644444']

此致 普拉迪普

3 个答案:

答案 0 :(得分:1)

您可以使用纯JavaScript执行此操作:

var result = jsonObj.filter(function(obj) {
  return obj.name === 'AAATest'
}).reduce(function(acc, obj) {
  var links = obj.links.filter(function(link) {
    return link.rel === 'self'
  }).map(function(link) {
    return link.href
  })
  return acc.concat(links)
},[])

console.log(result) //=> ["http://hostname:port/state/644444"]

答案 1 :(得分:1)

下划线解决方案:

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>

答案 2 :(得分:1)

使用下划线的另一个版本:

var result = _.chain(jsonObj)
    .where({ name: 'AAATest' })
    .pluck('links')
    .flatten()
    .where({ rel: 'self'})
    .pluck('href')
    .value();