【问题标题】:Converting existing Array into a new one将现有数组转换为新数组
【发布时间】:2019-03-12 17:17:21
【问题描述】:

我有一个数组,我正在将它变成一个适合我工作的新数组。这是我拥有的原始数组:

注意:“jayanagar”、“mallashwaram”、“kolar”在我的代码中被称为城市。

var array1=[
  ["Year", "2018-8", "2017-8"],
  ["JAYANAGAR", "2018-8", 10910665], 
  ["MALLESHWARAM", "2018-8", 2018451],
  ["KOLAR", "2018-8", 2277562], 
  ["JAYANAGAR", "2017-8", 1134]
]

然后我将它转换成这个:

var array2=[
  ["Year", "Jul 2018", "Jul 2017"], 
  ["JAYANAGAR", 10910665, 1134], 
  ["MALLESHWARAM", 2018451], 
  ["KOLAR", 2277562]
]

我想做的是

    1234563数据,即jayanagar
  • 所以我想做的是,每当上一个日期的城市数量较少时,我想为那个城市显示为0

  • 例如在array2 中,我没有malleshwaramkolar 的数据,所以我想在那里显示为0

  • 而用户选择的年份会给我所有城市的名称,因此我想在这里循环其他日期数据,即array2

  • 最高的日期总是会有所有城市的名字

我做了什么

var input = [
  ["Year", "2018-8", "2017-8"],
  ["JAYANAGAR", "2018-8", 10910665],
  ["MALLESHWARAM", "2018-8", 2018451],
  ["KOLAR", "2018-8", 2277562],
  ["JAYANAGAR", "2017-8", 1134]
]
var e = input[0]
var a = new Date(e[1]);

a.setMonth(a.getMonth());
a = a.toUTCString();

var c = a.split(' ');
e[1] = c[2] + " " + c[3];
a = new Date(e[2]);

a.setMonth(a.getMonth());
a = a.toUTCString();
c = a.split(' ');
e[2] = c[2] + " " + c[3];

const merged = input.reduce((acc, arr) => {
  const [city, year, value] = arr;

  if (city === "Year")
    acc[city] = arr
  else {
    acc[city] = acc[city] || [city]
    acc[city].push(value)
  }

  return acc;
}, {})

const output = Object.values(merged)
console.log(output)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我的预期输出

[
  [
    "Year",
    "Aug 2018",
    "Aug 2017"
  ],
  [
    "JAYANAGAR",
    10910665,
    1134
  ],
  [
    "MALLESHWARAM",
    2018451,
    0
  ],
  [
    "KOLAR",
    2277562,
    0
  ]
]

【问题讨论】:

  • 我相信 7 月是一年中的第 7 个月……不应该是 August 2018 等吗?
  • 您的代码中没有 JSON。 JSON 是一种类似于 JS 文字的文本格式。你所拥有的是一个数组数组。
  • @JackBashford 由于浏览器的依赖关系,它被视为 7 月
  • 它不是 JSON!
  • @AbhishekGautam 很好,我已经编辑了我的帖子

标签: javascript json


【解决方案1】:

此解决方案创建对象以通过相同的键存储所有数据,其中键是城市,然后创建另一个数组,该数组在所需的输出中格式化数组。

    var input = [
      ["Year", "2018-8", "2017-8"],
      ["JAYANAGAR", "2018-8", 10910665],
      ["MALLESHWARAM", "2018-8", 2018451],
      ["KOLAR", "2018-8", 2277562],
      ["JAYANAGAR", "2017-8", 1134]
    ];
    
   
  
  var cities = {};
  var years = input[0]; 

  
function formatDate(date, index){

if(index === 0){ //skip formatting, because it is not a date
  return date;
}
  dates = {
  '1' : 'Jan',
  '2' : 'Feb',
  '3' : 'Mar',
  '4' : 'Apr',
  '5' : 'May',
  '6' : 'Jun',
  '7' : 'Jul',
  '8' : 'Aug',
  '9' : 'Sep',
  '10': 'Oct',
  '11': 'Nov',
  '12': 'Dec'
  };

  let dateArr = date.split('-');

  return dates[dateArr[1]] + ' ' + dateArr[0];
 
}


years = years.map(formatDate); //run through dates array. 


  
  //store data in object items, where each key is city , and items are arrays of data
  input.forEach(function(element,index) {
    if(index !== 0){
    if( cities[element[0]] === undefined){
       cities[element[0]] = [];
    }
    
      cities[element[0]].push(element[2]); // add to city item new data
      
    }
  });
  

 var citiesData = []; 
 for (var city in cities) { //reformat the data in format [city, ...data]
    if (cities.hasOwnProperty(city)) {
      //  citiesData.push
     cities[city].unshift(city); //add city as first item in array
     if(years.length > cities[city].length){ //assuming that data should be available for each year, check the length and fill with zeroes 
     let fillsLength =  years.length -  cities[city].length;
     let fills = Array(fillsLength).fill(0, 0, fillsLength); //fill with zeros x times of missing data per each year
     cities[city] = cities[city].concat(fills); //concat array of zeros with city data
     }
     citiesData.push(cities[city]);
    }
}

citiesData.unshift(years);

console.log(citiesData); 

日期注意事项:与使用 Date 和 Date 方法相比,更简单的方法是使用包含每个月的缩写的对象作为值,将数字格式作为月份的键。

【讨论】:

  • 这里只有我卡住了,我如何检查年度城市和 putvalues 为 0
  • @dheerajkumar,第二级每个数组的第三项是什么?
  • [city, year, value] 这是我数组中的三项
  • @dheerajkumar,我已经用新代码编辑了我的答案。您需要在此解决方案中使用对象的帮助
  • @dheerajkumar,有什么疑问?运行 sn-p,检查结果。
猜你喜欢
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多