如何从json数据中获取独特的年份列表?

时间:2018-05-04 20:13:40

标签: javascript

我有以下json数据,想要获得仅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}]

我想获得一个只有年份列表的数组。我的预期输出是:

[2017, 2016, 2015]

我试着跟随:

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

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

但是出于某种奇怪的原因,我在console.log(lis_of_years)时得到了两个数组。你能来看看我在这里做错了什么吗?

5 个答案:

答案 0 :(得分:3)

您可以使用ES6 Set类来获取唯一值。

[... 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))])




答案 1 :(得分:3)

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

答案 2 :(得分:1)

在您的代码中,您不会检查重复项。您可以将对象映射到多年的数组,然后将其转换为Set和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);

另一种方法是reduce直接进入集合:

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);

答案 3 :(得分:1)

您可以将值映射为制作集合的值并获取其值。



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);




答案 4 :(得分:1)

要达到预期效果,请使用以下选项使用地图和过滤器

  1. map to get array of years
  2. 过滤以获取多年的唯一值

     var lis_of_years = data.map(val => val.financial_year)
                      .filter((v, i, self) => self.indexOf(v) === i)
    
  3. 代码示例 - 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)