Проверьте, работает ли это.
let data = [
{ a: "fin", year: 1996, value: 1 },
{ a: "fin", year: 1997, value: 2 },
{ a: "fin", year: 1998, value: 3 },
{ a: "fin", year: 1999, value: 4 },
{ a: "swe", year: 1996, value: 5 },
{ a: "swe", year: 1997, value: 6 },
{ a: "swe", year: 1998, value: 7 },
{ a: "swe", year: 1999, value: 8 }
]
let groupByCountries = {};
data.forEach(country => {
if (!groupByCountries.hasOwnProperty(country.a)) {
groupByCountries[country.a] = {
years: [country.year],
values: [country.value]
}
}
else {
groupByCountries[country.a].years.push(country.year);
groupByCountries[country.a].values.push(country.value);
}
})
let combinedValues = []
Object.keys(groupByCountries).forEach(country => {
combinedValues.push({
a: country,
years: groupByCountries[country].years,
values: groupByCountries[country].values
})
})
//just for printing
combinedValues.forEach((country) => {
console.log(country)
})
let data = [
{ a: "fin", year: 1996, value: 1 },
{ a: "fin", year: 1997, value: 2 },
{ a: "fin", year: 1998, value: 3 },
{ a: "fin", year: 1999, value: 4 },
{ a: "swe", year: 1996, value: 5 },
{ a: "swe", year: 1997, value: 6 },
{ a: "swe", year: 1998, value: 7 },
{ a: "swe", year: 1999, value: 8 }
]
let groupByCountries = {};
data.forEach(country => {
if (!groupByCountries.hasOwnProperty(country.a)) {
groupByCountries[country.a] = {
years: [country.year],
values: [country.value]
}
}
else {
groupByCountries[country.a].years.push(country.year);
groupByCountries[country.a].values.push(country.value);
}
})
let combinedValues = []
Object.keys(groupByCountries).forEach(country => {
combinedValues.push({
a: country,
years: groupByCountries[country].years,
values: groupByCountries[country].values
})
})
//just for printing
combinedValues.forEach((country) => {
console.log(country)
})