简体   繁体   English

从javascript中的JSON对象创建唯一的月份数组(无for循环)

[英]Create array of unique months from JSON object in javascript (without for loop)

I am learning d3 in javascript and am working on some basic data manipulation stuff. 我正在用javascript学习d3,正在研究一些基本的数据处理方面的东西。 I have a simple JSON object of year / month / births data that looks like this: 我有一个简单的JSON对象,包括年/月/出生数据,如下所示:

var birthData = [
  { 
    "year": 1967, 
    "month": "January", 
    "births": 31502 
  },
  { 
    "year": 1967, 
    "month": "January", 
    "births": 26703 
  },
  { 
    "year": 1967, 
    "month": "February", 
    "births": 28853 
  },
  { 
    "year": 1967, 
    "month": "February", 
    "births": 26958 
  },
  { 
    "year": 1967, 
    "month": "February", 
    "births": 28591 
  },
  { 
    "year": 1967, 
    "month": "March", 
    "births": 29545 
  },
  { 
    "year": 1967, 
    "month": "March", 
    "births": 30086 
  }]

and I would like to simply make an array of unique months that appear in this JSON object that looks like this: 并且我只想简单地制作一个数组,在这个JSON对象中显示如下所示的独特月份:

["January", "February", "March"]

I would prefer to avoid using this approach 我宁愿避免使用这种方法

var months = [];
for(var i = 0; i <= birthData.length; i++) {
  var month = birthData[i].month
  if(months.indexOf(month) === -1) {
    months.push(month)
  }
}

Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

EDIT: As a quick follow up question, are there any really good resources for learning data manipulation with javascript (particularly with d3 or other graphing libraries). 编辑:作为一个快速跟进的问题,是否有任何真正好的资源来学习使用javascript的数据操作(尤其是d3或其他图形库)。 Being very quick with data manip in javascript would make my life much easier! 用javascript进行数据处理非常快,这将使我的生活更加轻松!

Using reduce is a neat approach: 使用reduce是一个很好的方法:

var months = birthData.reduce((accumulator, item) => 
  accumulator.includes(item.month) 
    ? accumulator 
    : [ ...accumulator, item.month ], []);

What this does is it loops through all items while building a new array ( accumulator ) and, with a condition that checks if a month has already been added to this new array, determines if it should add the month of the current item to the array or not. 它的作用是在构建新数组( accumulator )时循环遍历所有项目,并在检查是否已将月份添加到此新数组的条件下,确定是否应将当前项目的月份添加到数组中或不。

The above code uses ES2015 syntax, here's the equivalent ES5 syntax: 上面的代码使用ES2015语法,这是等效的ES5语法:

var months = birthData.reduce(function(accumulator, item) { 
  return accumulator.indexOf(item.month) > -1
    ? accumulator 
    : accumulator.concat(item.month);
}, []);

You could map the months back to a new array, then use a Set, which only holds unique values, to make the array unique without iterating yourself 您可以将月份映射回新数组,然后使用仅保存唯一值的Set来使数组唯一而无需重复自己

var months = [...new Set(birthData.map(i => i.month))]

 var birthData = [{ "year": 1967, "month": "January", "births": 31502 }, { "year": 1967, "month": "January", "births": 26703 }, { "year": 1967, "month": "February", "births": 28853 }, { "year": 1967, "month": "February", "births": 26958 }, { "year": 1967, "month": "February", "births": 28591 }, { "year": 1967, "month": "March", "births": 29545 }, { "year": 1967, "month": "March", "births": 30086 } ] var months = [...new Set(birthData.map(i => i.month))] console.log(months) 

const months = birthData.map(month=>month.month);
const result = months.filter((item,pos)=>months.indexOf(item) === pos);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM