Я создал новый объект из массива объектов, чтобы позволить мне передавать данные. Он будет сгущать массив объектов в меньший массив объектов, который можно будет использовать для визуализации данных проще, чем раньше.
Я просто застрял.
Я дошел до этого, но я пойман сейчас и не могу понять, как действовать.
Это весь код:
window.onload = function () {
console.log('data', data)
const arrObj = data;
let mapPlaces = arrObj.map(i => i.place_name)
let uniquePlaces = [...new Set(mapPlaces)];
console.log('2place_name', arrObj.map(i => i.place_name))
console.log('uniquePlaces', uniquePlaces)
const newObj = uniquePlaces.map(v => {
return ({
location: v,
year: {
'10': '',
'11': '',
'12': '',
'13': '',
'14': '',
'15': '',
'16': '',
'17': '',
'18': '',
'19': '',
'95': '',
'96': '',
'97': '',
'98': '',
'99': '',
'00': '',
'01': '',
'02': '',
'03': '',
'04': '',
'05': '',
'06': '',
'07': '',
'08': '',
'09': ''
}
})
})
const finalObj = newObj.map(i => {
// console.log('i', i)
const result = arrObj.filter(j => {
// console.log('j', j)
if (j.place_name === i.location) {
const newPriceAdded = (
newObj.year[j.year] = j.price
)
console.log('newPriceAdded', newPriceAdded)
return newPriceAdded
}
})
const slicedResult = result.slice(0, 25)
return slicedResult;
})
console.log('finalObj', finalObj)
const myJSON = JSON.stringify(finalObj);
return document.getElementById("jsonDiv").innerHTML = myJSON;
}
Вот данные:
const data = [
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "95",
"price": 100
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "96",
"price": 103.19
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "97",
"price": 99.81
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "98",
"price": 102.7
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "99",
"price": 103.43
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "00",
"price": 103.91
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "01",
"price": 108.71
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "02",
"price": 115.59
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "03",
"price": 125.77
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "04",
"price": 140.02
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "05",
"price": 161.66
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "06",
"price": 183.55
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "07",
"price": 191.6
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "08",
"price": 195.04
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "09",
"price": 193.69
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "10",
"price": 186.51
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "11",
"price": 186.19
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "12",
"price": 185.14
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "13",
"price": 185.2
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "14",
"price": 181.7
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "15",
"price": 186.88
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "16",
"price": 190.23
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "17",
"price": 193.94
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "18",
"price": 202.51
},
{
"place_name": "Albany-Schenectady-Troy, NY",
"year": "19",
"price": 206.5
},
{
"place_name": "Binghamton, NY",
"year": "95",
"price": 100
},
{
"place_name": "Binghamton, NY",
"year": "96",
"price": 101.14
},
{
"place_name": "Binghamton, NY",
"year": "97",
"price": 97.59
},
{
"place_name": "Binghamton, NY",
"year": "98",
"price": 104.98
},
{
"place_name": "Binghamton, NY",
"year": "99",
"price": 109.23
},
{
"place_name": "Binghamton, NY",
"year": "00",
"price": 107.62
},
{
"place_name": "Binghamton, NY",
"year": "01",
"price": 116.33
},
{
"place_name": "Binghamton, NY",
"year": "02",
"price": 123.51
},
{
"place_name": "Binghamton, NY",
"year": "03",
"price": 127.99
},
{
"place_name": "Binghamton, NY",
"year": "04",
"price": 131.27
},
{
"place_name": "Binghamton, NY",
"year": "05",
"price": 138.92
},
{
"place_name": "Binghamton, NY",
"year": "06",
"price": 154.53
},
{
"place_name": "Binghamton, NY",
"year": "07",
"price": 166.31
},
{
"place_name": "Binghamton, NY",
"year": "08",
"price": 179.47
},
{
"place_name": "Binghamton, NY",
"year": "09",
"price": 181.03
},
{
"place_name": "Binghamton, NY",
"year": "10",
"price": 178.4
},
{
"place_name": "Binghamton, NY",
"year": "11",
"price": 169.56
},
{
"place_name": "Binghamton, NY",
"year": "12",
"price": 167.02
},
{
"place_name": "Binghamton, NY",
"year": "13",
"price": 173.3
},
{
"place_name": "Binghamton, NY",
"year": "14",
"price": 168.92
},
{
"place_name": "Binghamton, NY",
"year": "15",
"price": 166.03
},
{
"place_name": "Binghamton, NY",
"year": "16",
"price": 166.58
},
{
"place_name": "Binghamton, NY",
"year": "17",
"price": 165.71
},
{
"place_name": "Binghamton, NY",
"year": "18",
"price": 173.75
},
{
"place_name": "Binghamton, NY",
"year": "19",
"price": 177.04
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "95",
"price": 100
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "96",
"price": 105.5
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "97",
"price": 103.4
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "98",
"price": 105.8
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "99",
"price": 106.29
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "00",
"price": 102.95
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "01",
"price": 109.39
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "02",
"price": 113.54
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "03",
"price": 117.93
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "04",
"price": 123.44
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "05",
"price": 131.73
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "06",
"price": 136.78
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "07",
"price": 140.14
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "08",
"price": 143.76
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "09",
"price": 147.7
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "10",
"price": 146.41
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "11",
"price": 147.12
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "12",
"price": 149.02
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "13",
"price": 150.44
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "14",
"price": 155.2
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "15",
"price": 159.21
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "16",
"price": 164.4
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "17",
"price": 173.95
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "18",
"price": 185.39
},
{
"place_name": "Buffalo-Cheektowaga, NY",
"year": "19",
"price": 198.55
},
{
"place_name": "Elmira, NY",
"year": "95",
"price": 100
},
{
"place_name": "Elmira, NY",
"year": "96",
"price": 102.8
},
{
"place_name": "Elmira, NY",
"year": "97",
"price": 98.72
},
{
"place_name": "Elmira, NY",
"year": "98",
"price": 104.62
},
{
"place_name": "Elmira, NY",
"year": "99",
"price": 107.3
},
{
"place_name": "Elmira, NY",
"year": "00",
"price": 109.59
},
{
"place_name": "Elmira, NY",
"year": "01",
"price": 117.95
},
{
"place_name": "Elmira, NY",
"year": "02",
"price": 119.72
},
{
"place_name": "Elmira, NY",
"year": "03",
"price": 125.08
},
{
"place_name": "Elmira, NY",
"year": "04",
"price": 126.2
},
{
"place_name": "Elmira, NY",
"year": "05",
"price": 133.62
},
{
"place_name": "Elmira, NY",
"year": "06",
"price": 139.22
},
{
"place_name": "Elmira, NY",
"year": "07",
"price": 147.15
},
{
"place_name": "Elmira, NY",
"year": "08",
"price": 149.48
},
{
"place_name": "Elmira, NY",
"year": "09",
"price": 157.64
},
{
"place_name": "Elmira, NY",
"year": "10",
"price": 157.95
},
{
"place_name": "Elmira, NY",
"year": "11",
"price": 158.09
},
{
"place_name": "Elmira, NY",
"year": "12",
"price": 163.78
},
{
"place_name": "Elmira, NY",
"year": "13",
"price": 160.09
},
{
"place_name": "Elmira, NY",
"year": "14",
"price": 162.26
},
{
"place_name": "Elmira, NY",
"year": "15",
"price": 166.42
},
{
"place_name": "Elmira, NY",
"year": "16",
"price": 158.15
},
{
"place_name": "Elmira, NY",
"year": "17",
"price": 168.35
},
{
"place_name": "Elmira, NY",
"year": "18",
"price": 168.08
},
{
"place_name": "Elmira, NY",
"year": "19",
"price": 169.32
},
{
"place_name": "Glens Falls, NY",
"year": "95",
"price": 100
},
{
"place_name": "Glens Falls, NY",
"year": "96",
"price": 96.64
}
]
Мне пришлось удалить какую-то строку, чтобы соответствовать пределу тела. Делай как надо для редактирования. Мне нужно, чтобы объект выглядел так:
{
location: v,
year: {
'10': '',
'11': '',
'12': '',
'13': '',
'14': '',
'15': '',
'16': '',
'17': '',
'18': '',
'19': '',
'95': '',
'96': '',
'97': '',
'98': '',
'99': '',
'00': '',
'01': '',
'02': '',
'03': '',
'04': '',
'05': '',
'06': '',
'07': '',
'08': '',
'09': ''
}
}
Один для каждого местоположения.