![](/img/trans.png)
[英]How to convert a single key value pair object to array of objects with key value pair?
[英]how to make single object from an object array that has common key:value pair
我有一些数据
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}]
我想把它改造成
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
}
]
我试过使用这样的过滤器
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)
但结果不是我想要的,我得到以下
[
{
"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
}
]
这是我的代码,请运行以查看结果以及如何按照我的意愿制作
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)
我真的需要一些关于如何实现这一目标的见解,请任何人帮助。 我还附上了我当前实现的片段,以便很容易看到我在做什么
您可以使用数组 forEach 来实现您想要的
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);
您在每次迭代中为所有 object 属性分配值。 在每次迭代中,您将获得单个属性的值。 所有其他值将是未定义的。 所以剩余的值每次都会被 undefined 覆盖。
不要手动分配给所有值,而是使用temp[index][Object.keys(obj)[1]] = Object.values[obj][1])
我使用了 function 来减少一些代码。
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)
这是一个有效的JFiddle
我在这段代码中添加了一些注释。
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);
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.