最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter Records from JSON with Node or ES6 - Stack Overflow

programmeradmin8浏览0评论

I'm not sure the best way to go about this. I want to iterate my json and find all panies that are in the US for example. This JSON might get way more plex 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 panies in the USA?

[{
    "id": 0,
    "name": "Company1",
    "logoUrl": "/lib/assets/pany1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/pany2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/pany3-logo.png",
    "location":{
      "country": "USA",
      "state": "Michigan",
      "city": "Detroit"
    },
    "active": false
  }]

I'm not sure the best way to go about this. I want to iterate my json and find all panies that are in the US for example. This JSON might get way more plex 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 panies in the USA?

[{
    "id": 0,
    "name": "Company1",
    "logoUrl": "/lib/assets/pany1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/pany2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/pany3-logo.png",
    "location":{
      "country": "USA",
      "state": "Michigan",
      "city": "Detroit"
    },
    "active": false
  }]
Share Improve this question edited Jun 14, 2016 at 4:32 Pranav C Balan 115k25 gold badges171 silver badges195 bronze badges asked Jun 14, 2016 at 4:22 PositiveGuyPositiveGuy 20.2k30 gold badges93 silver badges151 bronze badges 2
  • yes I know that, but you can do node without ES6, I am simply saying use ES6 if you want for example or not, doesn't matter either way just trying to thing of ways to filter this – PositiveGuy Commented Jun 14, 2016 at 4:27
  • in other words use node middleware to help filter, or use plain JS methods, or Use fancy ES6 whatever you want for ideas or suggestions on this – PositiveGuy Commented Jun 14, 2016 at 4:28
Add a ment  | 

4 Answers 4

Reset to default 10

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/pany1-logo.png",
  "location": {
    "country": "USA",
    "state": "California",
    "city": "Napa"
  },
  "active": false
}, {
  "id": 1,
  "name": "Company2",
  "logoUrl": "/lib/assets/pany2-logo.png",
  "location": {
    "country": "Germany",
    "state": "",
    "city": "Berlin"
  },
  "active": false
}, {
  "id": 2,
  "name": "Company3",
  "logoUrl": "/lib/assets/pany3-logo.png",
  "location": {
    "country": "USA",
    "state": "Michigan",
    "city": "Detroit"
  },
  "active": false
}];

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

console.log(res);

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/pany1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/pany2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/pany3-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);

In lodash it will be

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

You can use native filter function.

const items = [{
    "id": 0,
    "name": "Company1",
    "logoUrl": "/lib/assets/pany1-logo.png",
    "location":{
      "country": "USA",
      "state": "California",
      "city": "Napa"
    },
    "active": false
  },
  {
    "id": 1,
    "name": "Company2",
    "logoUrl": "/lib/assets/pany2-logo.png",
    "location":{
      "country": "Germany",
      "state": "",
      "city": "Berlin"
    },
    "active": false
  },
  {
    "id": 2,
    "name": "Company3",
    "logoUrl": "/lib/assets/pany3-logo.png",
    "location":{
      "country": "USA",
      "state": "Michigan",
      "city": "Detroit"
    },
    "active": false
  }]

const usItems = items.filter(v => v.location.country === 'USA')
发布评论

评论列表(0)

  1. 暂无评论