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

JavaScript: How to filter data in an array - Stack Overflow

programmeradmin0浏览0评论

I have following object in JS:

[
  {
    "financial_year":1,
    "mainline_revenue":18743.0,
    "regional_revenue":2914.0,
    "other_revenue":3198.0,
    "non_operating_items":-1983.0
  },
  {
    "financial_year":2,
    "mainline_revenue":20218.0,
    "regional_revenue":3131.0,
    "other_revenue":3394.0,
    "non_operating_items":-3233.0
  },
  {
    "financial_year":3,
    "mainline_revenue":30802.0,
    "regional_revenue":6322.0,
    "other_revenue":5526.0,
    "non_operating_items":-1367.0
  }
]

financial_year is the unique identifier which I want to use to filter data. How can I filter data where for example financial_year is 2 and put the other values in an array?

I have following object in JS:

[
  {
    "financial_year":1,
    "mainline_revenue":18743.0,
    "regional_revenue":2914.0,
    "other_revenue":3198.0,
    "non_operating_items":-1983.0
  },
  {
    "financial_year":2,
    "mainline_revenue":20218.0,
    "regional_revenue":3131.0,
    "other_revenue":3394.0,
    "non_operating_items":-3233.0
  },
  {
    "financial_year":3,
    "mainline_revenue":30802.0,
    "regional_revenue":6322.0,
    "other_revenue":5526.0,
    "non_operating_items":-1367.0
  }
]

financial_year is the unique identifier which I want to use to filter data. How can I filter data where for example financial_year is 2 and put the other values in an array?

Share Improve this question edited Apr 15, 2022 at 0:06 Jason C 40.4k15 gold badges135 silver badges198 bronze badges asked Aug 18, 2017 at 15:42 HannanHannan 1,1916 gold badges24 silver badges40 bronze badges 4
  • 2 google: MDN array methods. You need filter. – dfsq Commented Aug 18, 2017 at 15:44
  • 4 Possible duplicate of How to filter an object with its values in ES6 – Atty Commented Aug 18, 2017 at 15:45
  • have you tried anything? – andrewgi Commented Aug 18, 2017 at 15:45
  • This is an array of objects. Not a dictionary. – Snazzie Commented Dec 29, 2021 at 18:31
Add a ment  | 

1 Answer 1

Reset to default 8

You can use the filter method on arrays. filter takes a callback which returns true or false (more accurately, a truthy or falsey value). If it returns true, that object is included in the resulting array.

let input = [
  {
    "financial_year":1,
    "mainline_revenue":18743.0,
    "regional_revenue":2914.0,
    "other_revenue":3198.0,
    "non_operating_items":-1983.0
  },
  {
    "financial_year":2,
    "mainline_revenue":20218.0,
    "regional_revenue":3131.0,
    "other_revenue":3394.0,
    "non_operating_items":-3233.0
  },
  {
    "financial_year":3,
    "mainline_revenue":30802.0,
    "regional_revenue":6322.0,
    "other_revenue":5526.0,
    "non_operating_items":-1367.0
  }
];
let output = input.filter((obj) => obj.financial_year !== 2);
console.log(JSON.stringify(output, null, 2));

Or rewritten with ES5:

var input = [
  {
    "financial_year":1,
    "mainline_revenue":18743.0,
    "regional_revenue":2914.0,
    "other_revenue":3198.0,
    "non_operating_items":-1983.0
  },
  {
    "financial_year":2,
    "mainline_revenue":20218.0,
    "regional_revenue":3131.0,
    "other_revenue":3394.0,
    "non_operating_items":-3233.0
  },
  {
    "financial_year":3,
    "mainline_revenue":30802.0,
    "regional_revenue":6322.0,
    "other_revenue":5526.0,
    "non_operating_items":-1367.0
  }
];
var output = input.filter(function(obj) {
  return obj.financial_year !== 2;
});
console.log(JSON.stringify(output, null, 2));

发布评论

评论列表(0)

  1. 暂无评论