gpt4 book ai didi

javascript - 将现有数组转换为新数组

转载 作者:搜寻专家 更新时间:2023-11-01 04:28:18 26 4
gpt4 key购买 nike

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

注意:“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]
]

我想做的是

  • 如您在 arra1 中所见,我有两年,即 2018-82017-8,日期 2018-8 我有所有城市的数据,但是日期 2017-08 只有一个城市数据,即 jayanagar

    /li>
  • 所以我想做的是,每当前一个日期的城市数量较少时,我想将该城市显示为 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
]
]

最佳答案

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

    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 方法,更简单的方法是使用包含每个月的缩写作为值的对象,以及作为键的月份的数字格式。

关于javascript - 将现有数组转换为新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55116331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com