0

I have a json from which I need to filter the objects whose name value matches the passed input and then pluck the href value from the corresponding links object having the ref value of self. I am attaching the code snippet as well as my approach of filtering. I am using 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());

Please let me know how to filter based on the given keyword:

Example input: AAATest Expected output: [href:'http://hostname:port/state/644444']

Regards, Pradeep

1
  • Seems like a good question but your description of what you're actually trying to map is beyond comprehension. Could you maybe make a point-form list of the data transformations you're trying to do? Commented Jan 27, 2015 at 16:33

3 Answers 3

1

Underscore solution:

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>

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with plain 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"]

Comments

1

Another version using underscore:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.