I have an student object with different fields.I need to format each of this object with data and the type of the data Below is the object
const data=[
{
"ID": 1,
"NAME": "SAM",
"GRADE": "1st GRADE",
"AGE": ,
"PERCENTAGE": 96.25,
},
{
"ID": 2,
"NAME": "JULIAN",
"GRADE": "3rd GRADE",
"AGE": ,
"PERCENTAGE": 90.33,
},
{
"ID": 3,
"NAME": "PETER",
"GRADE": "1st GRADE",
"AGE": ,
"PERCENTAGE": 91.23,
}
]
Goal is that individual object should be associated with data and type. Expected format is as below
[ {
"ID": {data:1,type:number},
"NAME": {data:"SAM",type:string},
"GRADE": {data:"1st GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:96.25,type:number},
},
{
"ID": {data:2,type:number},
"NAME": {data:"JULIAN",type:string},
"GRADE": {data:"3rd GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:90.33,type:number}
},
{
"ID": {data:3,type:number},
"NAME": {data:"PETER",type:string},
"GRADE": {data:"1st GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:91.23,type:number}
}
]
EDIT 1:I want to group by the data on the field id. Expected output should be
[
"1": [{
"ID": {data:1,type:number},
"NAME": {data:"SAM",type:string},
"GRADE": {data:"1st GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:96.25,type:number},
}],
"2":[
{
"ID": {data:2,type:number},
"NAME": {data:"JULIAN",type:string},
"GRADE": {data:"3rd GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:90.33,type:number}
}],
"3":[
{
"ID": {data:3,type:number},
"NAME": {data:"PETER",type:string},
"GRADE": {data:"1st GRADE",type:string},
"AGE": {data:,type:number}
"PERCENTAGE": {data:91.23,type:number}
}
]
]
I tried using
var outObject = data.reduce(function (a, e) {
let estKey = (e[ID.data]);
(a[estKey] ? a[estKey] : (a[estKey] = null || [])).push(e);
return a;
}, {});
return outObject
}
But the groupby isn't happening
AGEexample isn't correct.""is not anumber. It's also impossible to infer the type from that.Uncaught SyntaxError: Unexpected token ','"as it needs to have something afterAGE:and before the,. Any of the examples here will do: jsfiddle.net/5t18L0jo