JavaScript: How to filter data in dictionary -
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 unique identifier want use filter data. how can filter data example financial_year 2 , put other values in array?
you can use filter
method on arrays. filter
takes callback returns true or false (more accurately, truthy or falsey value). if returns true, object included in 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 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));
Comments
Post a Comment