Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Records from JSON with Node or ES6

I'm not sure the best way to go about this. I want to iterate my json and find all companies that are in the US for example. This JSON might get way more complex as my app grows too, as in levels, objects, etc. I just want to know ways people are doing simple searching for filtering out subsets of data with JSON and Node.js and/or ES6 or libraries maybe such as Lodash, etc.

So for example this json, what are some ways I can search it and pull back only those companies in the USA?

[{
    "id": 0,
    "name": "Company1",
    "logoUrl": "/lib/assets/company1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/company2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/company3-logo.png",
    "location":{
      "country": "USA",
      "state": "Michigan",
      "city": "Detroit"
    },
    "active": false
  }]
like image 670
PositiveGuy Avatar asked Jun 14 '16 04:06

PositiveGuy


3 Answers

Use JavaScript native Array#filter method with ES6 arrow function

var res = data.filter(v => v.location.country === 'USA');

var data = [{
  "id": 0,
  "name": "Company1",
  "logoUrl": "/lib/assets/company1-logo.png",
  "location": {
    "country": "USA",
    "state": "California",
    "city": "Napa"
  },
  "active": false
}, {
  "id": 1,
  "name": "Company2",
  "logoUrl": "/lib/assets/company2-logo.png",
  "location": {
    "country": "Germany",
    "state": "",
    "city": "Berlin"
  },
  "active": false
}, {
  "id": 2,
  "name": "Company3",
  "logoUrl": "/lib/assets/company3-logo.png",
  "location": {
    "country": "USA",
    "state": "Michigan",
    "city": "Detroit"
  },
  "active": false
}];

var res = data.filter(v => v.location.country === 'USA');

console.log(res);
like image 91
Pranav C Balan Avatar answered Oct 17 '22 01:10

Pranav C Balan


You can use JavaScript's simple .filter() method to return the list of results fulfilling the filter. Say your data is in variable data

ES5

data.filter(function(item) {
  return item.location.country === 'USA';
});

ES6: In ES6 you can use arrow functions for same as

data.filter((item) => {
  return item.location.country === 'USA';
});

var data = [{
    "id": 0,
    "name": "Company1",
    "logoUrl": "/lib/assets/company1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/company2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/company3-logo.png",
    "location":{
      "country": "USA",
      "state": "Michigan",
      "city": "Detroit"
    },
    "active": false
  }];

var res1 = data.filter(function(item) {
  return item.location.country === 'USA';
});

const res2 = data.filter((item) => {
  return item.location.country === 'USA';
});

console.log(res1);
console.log(res2);
like image 30
Aditya Singh Avatar answered Oct 16 '22 23:10

Aditya Singh


In lodash it will be

_.filter(data, function(item) {
  return item.location.country === 'USA';
});
like image 25
Nafiul Islam Avatar answered Oct 17 '22 00:10

Nafiul Islam