我有一个要变成适合我工作的新数组。这是我原来拥有的Array:
注意:在我的代码中,“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-8
和2017-8
,对于日期2018-8
我拥有所有城市的数据,但对于日期2017-08
来说只有一个城市数据,即jayanagar
0
array2
中,我没有malleshwaram
和kolar
的数据,因此我想在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/