Hannan
Hannan

Reputation: 1191

how to get unique list of years from json data?

I have following json data and want to get an array of only financial_year. It has to be only unique years.

var data = [{"financial_year": 2017, "revenue": 2000, "costs": 1000}, {"financial_year": 2017, "revenue": 3000, "costs": 2000}, {"financial_year": 2016, "revenue": 1000, "costs": 500},{"financial_year": 2016, "revenue": 2000, "costs": 1000}, {"financial_year": 2015, "revenue": 5000, "costs": 3000}, {"financial_year": 2015, "revenue": 2000, "costs": 1000}]

I want to get an array with a list of only years. My expected output is:

[2017, 2016, 2015]

I tried to fo following:

var years = []
data.forEach((val, index) => {
    years.push(val.financial_year)
});

var lis_of_years = Array.from(new Set(years))

But for some weird reason, I'm getting two arrays when I console.log(lis_of_years). Could you please check what am I doing wrong here?

Upvotes: 2

Views: 842

Answers (5)

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result , use below option of using map and filter

  1. map to get array of years
  2. filter to get unique values of years

     var lis_of_years = data.map(val => val.financial_year)
                      .filter((v, i, self) => self.indexOf(v) === i)
    

code sample - https://codepen.io/nagasai/pen/gzxNQN?editors=1010

var data = [{"financial_year": 2017, "revenue": 2000, "costs": 1000}, {"financial_year": 2017, "revenue": 3000, "costs": 2000}, {"financial_year": 2016, "revenue": 1000, "costs": 500},{"financial_year": 2016, "revenue": 2000, "costs": 1000}, {"financial_year": 2015, "revenue": 5000, "costs": 3000}, {"financial_year": 2015, "revenue": 2000, "costs": 1000}]



var lis_of_years = data.map(val => val.financial_year)
                       .filter((v, i, self) => self.indexOf(v) === i)

console.log(lis_of_years)

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386746

You could map the values as value for making a set and take the values of it.

var data = [{ financial_year: 2017, revenue: 2000, costs: 1000 }, { financial_year: 2017, revenue: 3000, costs: 2000 }, { financial_year: 2016, revenue: 1000, costs: 500 }, { financial_year: 2016, revenue: 2000, costs: 1000 }, { financial_year: 2015, revenue: 5000, costs: 3000 }, { financial_year: 2015, revenue: 2000, costs: 1000 }],
    years = Array.from(new Set(data.map(({ financial_year }) => financial_year)));

console.log(years);

Upvotes: 1

djfdev
djfdev

Reputation: 6037

const years = data.map(obj => obj.financial_year)
const uniqueYears = [...new Set(years)]

Upvotes: 3

You could use ES6 Set class to get the unique values.

[... new Set(data.map(a=>a.financial_year))]

var data = [{"financial_year": 2017, "revenue": 2000, "costs": 1000}, {"financial_year": 2017, "revenue": 3000, "costs": 2000}, {"financial_year": 2016, "revenue": 1000, "costs": 500},{"financial_year": 2016, "revenue": 2000, "costs": 1000}, {"financial_year": 2015, "revenue": 5000, "costs": 3000}, {"financial_year": 2015, "revenue": 2000, "costs": 1000}]

console.log([... new Set(data.map(a=>a.financial_year))])

Upvotes: 3

CertainPerformance
CertainPerformance

Reputation: 371049

In your code, you're not checking for duplicates. You can map the objects to an array of years, then turn it into a Set and back:

const input = [{
  "financial_year": 2017,
  "revenue": 2000,
  "costs": 1000
}, {
  "financial_year": 2017,
  "revenue": 3000,
  "costs": 2000
}, {
  "financial_year": 2016,
  "revenue": 1000,
  "costs": 500
}, {
  "financial_year": 2016,
  "revenue": 2000,
  "costs": 1000
}, {
  "financial_year": 2015,
  "revenue": 5000,
  "costs": 3000
}, {
  "financial_year": 2015,
  "revenue": 2000,
  "costs": 1000
}];
const years = input.map(({ financial_year }) => financial_year);
const dedupedYears = [...new Set(years)];
console.log(dedupedYears);

Another method would be to reduce into a set directly:

const input = [{
  "financial_year": 2017,
  "revenue": 2000,
  "costs": 1000
}, {
  "financial_year": 2017,
  "revenue": 3000,
  "costs": 2000
}, {
  "financial_year": 2016,
  "revenue": 1000,
  "costs": 500
}, {
  "financial_year": 2016,
  "revenue": 2000,
  "costs": 1000
}, {
  "financial_year": 2015,
  "revenue": 5000,
  "costs": 3000
}, {
  "financial_year": 2015,
  "revenue": 2000,
  "costs": 1000
}];
const yearsSet = input.reduce((accum, { financial_year }) => accum.add(financial_year), new Set());
const dedupedYears = [...yearsSet];
console.log(dedupedYears);

Upvotes: 1

Related Questions