Reputation: 324
I have some data
var list =[{date: "2019-09-27T08:00", atmosphericpressure: 80.58},
{date: "2019-09-27T08:00", lightningdistance: 0},
{date: "2019-09-27T08:00", lightningevents: 0},
{date: "2019-09-27T08:00", precipitation: 0},
{date: "2019-09-27T08:00", radiation: 829.03},
{date: "2019-09-27T08:00", relativehumidity: 0.78},
{date: "2019-09-27T08:00", temperature: 20.24},
{date: "2019-09-27T08:00", winddirection: 56.43},
{date: "2019-09-27T08:00", windgusts: 3.78},
{date: "2019-09-27T08:00", windspeed: 1.73},
{date: "2019-09-27T09:00", atmosphericpressure: 80.5},
{date: "2019-09-27T09:00", lightningdistance: 0},
{date: "2019-09-27T09:00", lightningevents: 0},
{date: "2019-09-27T09:00", precipitation: 0},
{date: "2019-09-27T09:00", radiation: 739.98},
{date: "2019-09-27T09:00", relativehumidity: 0.74},
{date: "2019-09-27T09:00", temperature: 20.82},
{date: "2019-09-27T09:00", winddirection: 52.83},
{date: "2019-09-27T09:00", windgusts: 2.95},
{date: "2019-09-27T09:00", windspeed: 1.54}]
and I would like to transform it to
var list =[
{
date: '2019-09-27T08:00',
atmosphericpressure: 80.58,
lightningdistance: 0,
lightningevents: 0,
precipitation: 0,
radiation: 829.03,
relativehumidity: 0.78,
temperature: 20.24,
winddirection: 56.43,
windgusts: 3.78,
windspeed: 1.73
},
{
date: '2019-09-27T09:00',
atmosphericpressure: 80.5,
lightningdistance: 0,
lightningevents: 0,
precipitation: 0,
radiation: 739.98,
relativehumidity: 0.74,
temperature: 22.82,
winddirection: 52.83,
windgusts: 2.95,
windspeed: 1.54
}
]
I have have tried using filters like so
let temp=[]
let result = list.filter(obj => {
if(obj.date === '2019-09-27T09:00'){
let group= {
date:obj.date,
atmosphericpressure:obj.atmosphericpressure,
lightningdistance:obj.lightningdistance,
lightningevents:obj.lightningevents,
precipitation:obj.precipitation,
radiation:obj.radiation,
relativehumidity:obj.relativehumidity,
temperature:obj.temperature,
winddirection:obj.winddirection,
windgusts:obj.windgusts,
windspeed:obj.windspeed
}
temp.push(group)
}
})
console.log(temp)
but the result is not as I would like, I get the following
[
{
"date": "2019-09-27T09:00",
"atmosphericpressure": 80.5,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": 0,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": 0,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": 0,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": 739.98,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": 0.74,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": 20.82,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": 52.83,
"windgusts": undefined,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": 2.95,
"windspeed": undefined
},
{
"date": "2019-09-27T09:00",
"atmosphericpressure": undefined,
"lightningdistance": undefined,
"lightningevents": undefined,
"precipitation": undefined,
"radiation": undefined,
"relativehumidity": undefined,
"temperature": undefined,
"winddirection": undefined,
"windgusts": undefined,
"windspeed": 1.54
}
]
This is my code, please run to see results and how It can be made to as I would like
var list = [{date: "2019-09-27T08:00", atmosphericpressure: 80.58},
{date: "2019-09-27T08:00", lightningdistance: 0},
{date: "2019-09-27T08:00", lightningevents: 0},
{date: "2019-09-27T08:00", precipitation: 0},
{date: "2019-09-27T08:00", radiation: 829.03},
{date: "2019-09-27T08:00", relativehumidity: 0.78},
{date: "2019-09-27T08:00", temperature: 20.24},
{date: "2019-09-27T08:00", winddirection: 56.43},
{date: "2019-09-27T08:00", windgusts: 3.78},
{date: "2019-09-27T08:00", windspeed: 1.73},
{date: "2019-09-27T09:00", atmosphericpressure: 80.5},
{date: "2019-09-27T09:00", lightningdistance: 0},
{date: "2019-09-27T09:00", lightningevents: 0},
{date: "2019-09-27T09:00", precipitation: 0},
{date: "2019-09-27T09:00", radiation: 739.98},
{date: "2019-09-27T09:00", relativehumidity: 0.74},
{date: "2019-09-27T09:00", temperature: 20.82},
{date: "2019-09-27T09:00", winddirection: 52.83},
{date: "2019-09-27T09:00", windgusts: 2.95},
{date: "2019-09-27T09:00", windspeed: 1.54},
{date: "2019-09-27T10:00", atmosphericpressure: 80.37},
{date: "2019-09-27T10:00", lightningdistance: 0},
{date: "2019-09-27T10:00", lightningevents: 0},
{date: "2019-09-27T10:00", precipitation: 0},
{date: "2019-09-27T10:00", radiation: 662.45},
{date: "2019-09-27T10:00", relativehumidity: 0.69},
{date: "2019-09-27T10:00", temperature: 21.64},
{date: "2019-09-27T10:00", winddirection: 68.55},
{date: "2019-09-27T10:00", windgusts: 2.59},
{date: "2019-09-27T10:00", windspeed: 1.17},
{date: "2019-09-27T11:00", atmosphericpressure: 80.3},
{date: "2019-09-27T11:00", lightningdistance: 0},
{date: "2019-09-27T11:00", lightningevents: 0},
{date: "2019-09-27T11:00", precipitation: 0},
{date: "2019-09-27T11:00", radiation: 292.93},
{date: "2019-09-27T11:00", relativehumidity: 0.66},
{date: "2019-09-27T11:00", temperature: 21.31},
{date: "2019-09-27T11:00", winddirection: 47.26},
{date: "2019-09-27T11:00", windgusts: 2.74},
{date: "2019-09-27T11:00", windspeed: 1.24},
{date: "2019-09-27T12:00", atmosphericpressure: 80.24},
{date: "2019-09-27T12:00", lightningdistance: 0},
{date: "2019-09-27T12:00", lightningevents: 0},
{date: "2019-09-27T12:00", precipitation: 0},
{date: "2019-09-27T12:00", radiation: 168.85},
{date: "2019-09-27T12:00", relativehumidity: 0.65},
{date: "2019-09-27T12:00", temperature: 21.23},
{date: "2019-09-27T12:00", winddirection: 46.44},
{date: "2019-09-27T12:00", windgusts: 1.77},
{date: "2019-09-27T12:00", windspeed: 0.87},
{date: "2019-09-27T13:00", atmosphericpressure: 80.22},
{date: "2019-09-27T13:00", lightningdistance: 0},
{date: "2019-09-27T13:00", lightningevents: 0},
{date: "2019-09-27T13:00", precipitation: 0},
{date: "2019-09-27T13:00", radiation: 133.87},
{date: "2019-09-27T13:00", relativehumidity: 0.65},
{date: "2019-09-27T13:00", temperature: 20.89},
{date: "2019-09-27T13:00", winddirection: 44.6},
{date: "2019-09-27T13:00", windgusts: 1.7},
{date: "2019-09-27T13:00", windspeed: 1.02},
{date: "2019-09-27T14:00", atmosphericpressure: 80.25},
{date: "2019-09-27T14:00", lightningdistance: 0},
{date: "2019-09-27T14:00", lightningevents: 0},
{date: "2019-09-27T14:00", precipitation: 0},
{date: "2019-09-27T14:00", radiation: 79.42},
{date: "2019-09-27T14:00", relativehumidity: 0.65},
{date: "2019-09-27T14:00", temperature: 20.56},
{date: "2019-09-27T14:00", winddirection: 48.5},
{date: "2019-09-27T14:00", windgusts: 1.79},
{date: "2019-09-27T14:00", windspeed: 0.95},
{date: "2019-09-27T15:00", atmosphericpressure: 80.3},
{date: "2019-09-27T15:00", lightningdistance: 0},
{date: "2019-09-27T15:00", lightningevents: 0},
{date: "2019-09-27T15:00", precipitation: 0},
{date: "2019-09-27T15:00", radiation: 16.2},
{date: "2019-09-27T15:00", relativehumidity: 0.7},
{date: "2019-09-27T15:00", temperature: 19.46},
{date: "2019-09-27T15:00", winddirection: 352.4},
{date: "2019-09-27T15:00", windgusts: 0.68},
{date: "2019-09-27T15:00", windspeed: 0.42},
{date: "2019-09-27T16:00", atmosphericpressure: 80.37},
{date: "2019-09-27T16:00", lightningdistance: 0},
{date: "2019-09-27T16:00", lightningevents: 0},
{date: "2019-09-27T16:00", precipitation: 0},
{date: "2019-09-27T16:00", radiation: 0},
{date: "2019-09-27T16:00", relativehumidity: 0.77},
{date: "2019-09-27T16:00", temperature: 18.43},
{date: "2019-09-27T16:00", winddirection: 333.37},
{date: "2019-09-27T16:00", windgusts: 1.73},
{date: "2019-09-27T16:00", windspeed: 0.89},
{date: "2019-09-27T17:00", atmosphericpressure: 80.41},
{date: "2019-09-27T17:00", lightningdistance: 0},
{date: "2019-09-27T17:00", lightningevents: 0},
{date: "2019-09-27T17:00", precipitation: 0},
{date: "2019-09-27T17:00", radiation: 0},
{date: "2019-09-27T17:00", relativehumidity: 0.85},
{date: "2019-09-27T17:00", temperature: 17.42},
{date: "2019-09-27T17:00", winddirection: 353.25},
{date: "2019-09-27T17:00", windgusts: 0.94},
{date: "2019-09-27T17:00", windspeed: 0.46}
];
let temp=[]
let result = list.filter(obj => {
if(obj.date === '2019-09-27T09:00'){
let group= {
date:obj.date,
atmosphericpressure:obj.atmosphericpressure,
lightningdistance:obj.lightningdistance,
lightningevents:obj.lightningevents,
precipitation:obj.precipitation,
radiation:obj.radiation,
relativehumidity:obj.relativehumidity,
temperature:obj.temperature,
winddirection:obj.winddirection,
windgusts:obj.windgusts,
windspeed:obj.windspeed
}
temp.push(group)
}
})
console.log(temp)
I would really need some insights on how to achieve this, anyone please help. I have also attached a snipped of my current implementation so that it becomes easy to see what I am doing
Upvotes: 0
Views: 59
Reputation: 51
var list = [{
date: "2019-09-27T08:00",
atmosphericpressure: 80.58
},
{
date: "2019-09-27T08:00",
lightningdistance: 0
},
{
date: "2019-09-27T08:00",
lightningevents: 0
},
{
date: "2019-09-27T08:00",
precipitation: 0
},
{
date: "2019-09-27T08:00",
radiation: 829.03
},
{
date: "2019-09-27T08:00",
relativehumidity: 0.78
},
{
date: "2019-09-27T08:00",
temperature: 20.24
},
{
date: "2019-09-27T08:00",
winddirection: 56.43
},
{
date: "2019-09-27T08:00",
windgusts: 3.78
},
{
date: "2019-09-27T08:00",
windspeed: 1.73
},
{
date: "2019-09-27T09:00",
atmosphericpressure: 80.5
},
{
date: "2019-09-27T09:00",
lightningdistance: 0
},
{
date: "2019-09-27T09:00",
lightningevents: 0
},
{
date: "2019-09-27T09:00",
precipitation: 0
},
{
date: "2019-09-27T09:00",
radiation: 739.98
},
{
date: "2019-09-27T09:00",
relativehumidity: 0.74
},
{
date: "2019-09-27T09:00",
temperature: 20.82
},
{
date: "2019-09-27T09:00",
winddirection: 52.83
},
{
date: "2019-09-27T09:00",
windgusts: 2.95
},
{
date: "2019-09-27T09:00",
windspeed: 1.54
}
]
let temp = []
let result = list.filter(obj => {
let dateExist = temp.find(item => item.date && item.date === obj.date)
let tempObj = {}
if (!dateExist) {
Object.keys(obj).map((key, i) => {
tempObj[key] = obj[key]
})
temp.push(tempObj)
} else {
Object.keys(obj).map((key, i) => {
dateExist[key] = obj[key]
})
}
})
console.log(temp)
Upvotes: 0
Reputation: 12478
You're assigning values to all object properties at every iteration. In each iteration, you'll get value of single property. All other values will be undefined. So the remaining values will be overwritten with undefined every time.
Instead of manually assigning to all values, use temp[index][Object.keys(obj)[1]] = Object.values[obj][1])
I used a function to reduce some codes.
let temp=[];
function addData(pos,obj){
// a function to add the property to object at given position
temp[pos][Object.keys(obj)[1]] = Object.values(obj)[1];
}
list.forEach((o) => { // Initialize the array with available dates
if(!temp.find((d)=> d.date==o.date)){
temp.push({"date":o.date});
}
});
list.forEach((o) => { // Looping through each object
temp.forEach((d) => { // looping through temp to find the position in temp
if(o.date == d.date){
window.addData(temp.indexOf(d),o);
}
});
});
console.log(temp)
var list = [{date: "2019-09-27T08:00", atmosphericpressure: 80.58},
{date: "2019-09-27T08:00", lightningdistance: 0},
{date: "2019-09-27T08:00", lightningevents: 0},
{date: "2019-09-27T08:00", precipitation: 0},
{date: "2019-09-27T08:00", radiation: 829.03},
{date: "2019-09-27T08:00", relativehumidity: 0.78},
{date: "2019-09-27T08:00", temperature: 20.24},
{date: "2019-09-27T08:00", winddirection: 56.43},
{date: "2019-09-27T08:00", windgusts: 3.78},
{date: "2019-09-27T08:00", windspeed: 1.73},
{date: "2019-09-27T09:00", atmosphericpressure: 80.5},
{date: "2019-09-27T09:00", lightningdistance: 0},
{date: "2019-09-27T09:00", lightningevents: 0},
{date: "2019-09-27T09:00", precipitation: 0},
{date: "2019-09-27T09:00", radiation: 739.98},
{date: "2019-09-27T09:00", relativehumidity: 0.74},
{date: "2019-09-27T09:00", temperature: 20.82},
{date: "2019-09-27T09:00", winddirection: 52.83},
{date: "2019-09-27T09:00", windgusts: 2.95},
{date: "2019-09-27T09:00", windspeed: 1.54},
{date: "2019-09-27T10:00", atmosphericpressure: 80.37},
{date: "2019-09-27T10:00", lightningdistance: 0},
{date: "2019-09-27T10:00", lightningevents: 0},
{date: "2019-09-27T10:00", precipitation: 0},
{date: "2019-09-27T10:00", radiation: 662.45},
{date: "2019-09-27T10:00", relativehumidity: 0.69},
{date: "2019-09-27T10:00", temperature: 21.64},
{date: "2019-09-27T10:00", winddirection: 68.55},
{date: "2019-09-27T10:00", windgusts: 2.59},
{date: "2019-09-27T10:00", windspeed: 1.17},
{date: "2019-09-27T11:00", atmosphericpressure: 80.3},
{date: "2019-09-27T11:00", lightningdistance: 0},
{date: "2019-09-27T11:00", lightningevents: 0},
{date: "2019-09-27T11:00", precipitation: 0},
{date: "2019-09-27T11:00", radiation: 292.93},
{date: "2019-09-27T11:00", relativehumidity: 0.66},
{date: "2019-09-27T11:00", temperature: 21.31},
{date: "2019-09-27T11:00", winddirection: 47.26},
{date: "2019-09-27T11:00", windgusts: 2.74},
{date: "2019-09-27T11:00", windspeed: 1.24},
{date: "2019-09-27T12:00", atmosphericpressure: 80.24},
{date: "2019-09-27T12:00", lightningdistance: 0},
{date: "2019-09-27T12:00", lightningevents: 0},
{date: "2019-09-27T12:00", precipitation: 0},
{date: "2019-09-27T12:00", radiation: 168.85},
{date: "2019-09-27T12:00", relativehumidity: 0.65},
{date: "2019-09-27T12:00", temperature: 21.23},
{date: "2019-09-27T12:00", winddirection: 46.44},
{date: "2019-09-27T12:00", windgusts: 1.77},
{date: "2019-09-27T12:00", windspeed: 0.87},
{date: "2019-09-27T13:00", atmosphericpressure: 80.22},
{date: "2019-09-27T13:00", lightningdistance: 0},
{date: "2019-09-27T13:00", lightningevents: 0},
{date: "2019-09-27T13:00", precipitation: 0},
{date: "2019-09-27T13:00", radiation: 133.87},
{date: "2019-09-27T13:00", relativehumidity: 0.65},
{date: "2019-09-27T13:00", temperature: 20.89},
{date: "2019-09-27T13:00", winddirection: 44.6},
{date: "2019-09-27T13:00", windgusts: 1.7},
{date: "2019-09-27T13:00", windspeed: 1.02},
{date: "2019-09-27T14:00", atmosphericpressure: 80.25},
{date: "2019-09-27T14:00", lightningdistance: 0},
{date: "2019-09-27T14:00", lightningevents: 0},
{date: "2019-09-27T14:00", precipitation: 0},
{date: "2019-09-27T14:00", radiation: 79.42},
{date: "2019-09-27T14:00", relativehumidity: 0.65},
{date: "2019-09-27T14:00", temperature: 20.56},
{date: "2019-09-27T14:00", winddirection: 48.5},
{date: "2019-09-27T14:00", windgusts: 1.79},
{date: "2019-09-27T14:00", windspeed: 0.95},
{date: "2019-09-27T15:00", atmosphericpressure: 80.3},
{date: "2019-09-27T15:00", lightningdistance: 0},
{date: "2019-09-27T15:00", lightningevents: 0},
{date: "2019-09-27T15:00", precipitation: 0},
{date: "2019-09-27T15:00", radiation: 16.2},
{date: "2019-09-27T15:00", relativehumidity: 0.7},
{date: "2019-09-27T15:00", temperature: 19.46},
{date: "2019-09-27T15:00", winddirection: 352.4},
{date: "2019-09-27T15:00", windgusts: 0.68},
{date: "2019-09-27T15:00", windspeed: 0.42},
{date: "2019-09-27T16:00", atmosphericpressure: 80.37},
{date: "2019-09-27T16:00", lightningdistance: 0},
{date: "2019-09-27T16:00", lightningevents: 0},
{date: "2019-09-27T16:00", precipitation: 0},
{date: "2019-09-27T16:00", radiation: 0},
{date: "2019-09-27T16:00", relativehumidity: 0.77},
{date: "2019-09-27T16:00", temperature: 18.43},
{date: "2019-09-27T16:00", winddirection: 333.37},
{date: "2019-09-27T16:00", windgusts: 1.73},
{date: "2019-09-27T16:00", windspeed: 0.89},
{date: "2019-09-27T17:00", atmosphericpressure: 80.41},
{date: "2019-09-27T17:00", lightningdistance: 0},
{date: "2019-09-27T17:00", lightningevents: 0},
{date: "2019-09-27T17:00", precipitation: 0},
{date: "2019-09-27T17:00", radiation: 0},
{date: "2019-09-27T17:00", relativehumidity: 0.85},
{date: "2019-09-27T17:00", temperature: 17.42},
{date: "2019-09-27T17:00", winddirection: 353.25},
{date: "2019-09-27T17:00", windgusts: 0.94},
{date: "2019-09-27T17:00", windspeed: 0.46}
];
let temp=[];
function addData(pos,obj){
temp[pos][Object.keys(obj)[1]] = Object.values(obj)[1];
}
list.forEach((o) => {
if(!temp.find((d)=> d.date==o.date)){
temp.push({"date":o.date});
}
});
list.forEach((o) => {
temp.forEach((d) => {
if(o.date == d.date){
window.addData(temp.indexOf(d),o);
}
});
});
console.log(temp)
Upvotes: 0
Reputation: 3234
Here is a working JFiddle
I added in this code with some comments.
let newList = [];
let currKey = "";
let currObj = {};
list.forEach(function(e,i) {
if(currKey != e.date) {//How we will identify
currKey = e.date;
if(i != 0) //Prevents empty first space
newList[newList.length] = currObj; //Adds object to new list
currObj = {}; //Gives new starting point
currObj.date = e.date;
}
if(currKey == e.date) {
Object.keys(e).forEach(function(prop) {
if(e != 'date')
currObj[prop] = e[prop]; //Copies over the value
});
}
if(i == list.length-1)
newList[newList.length] = currObj; //Adds last object to list
});
console.log(newList);
Upvotes: 0
Reputation: 1
You can use array forEach to achieve what you want
var list =[{date: "2019-09-27T08:00", atmosphericpressure: 80.58},
{date: "2019-09-27T08:00", lightningdistance: 0},
{date: "2019-09-27T08:00", lightningevents: 0},
{date: "2019-09-27T08:00", precipitation: 0},
{date: "2019-09-27T08:00", radiation: 829.03},
{date: "2019-09-27T08:00", relativehumidity: 0.78},
{date: "2019-09-27T08:00", temperature: 20.24},
{date: "2019-09-27T08:00", winddirection: 56.43},
{date: "2019-09-27T08:00", windgusts: 3.78},
{date: "2019-09-27T08:00", windspeed: 1.73},
{date: "2019-09-27T09:00", atmosphericpressure: 80.5},
{date: "2019-09-27T09:00", lightningdistance: 0},
{date: "2019-09-27T09:00", lightningevents: 0},
{date: "2019-09-27T09:00", precipitation: 0},
{date: "2019-09-27T09:00", radiation: 739.98},
{date: "2019-09-27T09:00", relativehumidity: 0.74},
{date: "2019-09-27T09:00", temperature: 20.82},
{date: "2019-09-27T09:00", winddirection: 52.83},
{date: "2019-09-27T09:00", windgusts: 2.95},
{date: "2019-09-27T09:00", windspeed: 1.54}]
const result = [];
list.forEach(i => {
let x = result.find(r => r.date === i.date);
if(x) {
Object.assign(x, i);
} else {
result.push(i);
}
});
console.log(result);
Upvotes: 2