Im new to javaScript and react..
i have an array consisting of objects. I need to display this array in a AG GRID so what im tring here to map this to array of flat obj.
NOTE: I cannot use jQuery..
below is my array (full code is in codepen, please check the link)
let realData = [
[
{
"key" : "flightNumber" ,
"label": "Flight Number",
"value": "EK 131",
"value-type": "String",
"order" : 1
},
{
"key" : "flightId",
"label": "flightId",
"value": "1223434",
"value-type": "String",
"order" : 2,
"isId": true
},
{
"key": "CallSign",
"label": "callSign",
"value": "123",
"value-type": "String",
"order" : 3
},
]
]
what i want the result is
let newArray = [
{flightNumber: 'EK 131', flightType: 'A', lastUpdate: '11223232', destOrig: 'BEY', sibtSobt: '12121333123', CallSign: '123'},
{flightNumber: 'EK 132', flightType: 'D', lastUpdate: '11334455', destOrig: 'DEST', sibtSobt: '1234443222', CallSign: '443'}
]
please help
link
Thank you
Im new to javaScript and react..
i have an array consisting of objects. I need to display this array in a AG GRID so what im tring here to map this to array of flat obj.
NOTE: I cannot use jQuery..
below is my array (full code is in codepen, please check the link)
let realData = [
[
{
"key" : "flightNumber" ,
"label": "Flight Number",
"value": "EK 131",
"value-type": "String",
"order" : 1
},
{
"key" : "flightId",
"label": "flightId",
"value": "1223434",
"value-type": "String",
"order" : 2,
"isId": true
},
{
"key": "CallSign",
"label": "callSign",
"value": "123",
"value-type": "String",
"order" : 3
},
]
]
what i want the result is
let newArray = [
{flightNumber: 'EK 131', flightType: 'A', lastUpdate: '11223232', destOrig: 'BEY', sibtSobt: '12121333123', CallSign: '123'},
{flightNumber: 'EK 132', flightType: 'D', lastUpdate: '11334455', destOrig: 'DEST', sibtSobt: '1234443222', CallSign: '443'}
]
please help
link
Thank you
Share Improve this question edited Feb 26, 2018 at 11:34 Shubham Khatri 282k58 gold badges431 silver badges411 bronze badges asked Feb 26, 2018 at 11:10 Mohamed RihanMohamed Rihan 1192 silver badges15 bronze badges 5- What have you tried ? I remend you take the tour: stackoverflow./tour – Christophe Roussy Commented Feb 26, 2018 at 11:15
- please see this codepen.io/re5ive/pen/qxJxEq?editors=0012 – Mohamed Rihan Commented Feb 26, 2018 at 11:16
- Please include all important information directly into the question. See the help center for question requirements! – baao Commented Feb 26, 2018 at 11:17
-
There are some keys that are expected to be values? There are value and keys that didn't exist in JSON but do in result?
sibtSobt
? – zer00ne Commented Feb 26, 2018 at 11:17 - @zer00ne i cannot put all the data code in the editer.. so i put tham in codepen and attach the link. – Mohamed Rihan Commented Feb 26, 2018 at 11:19
3 Answers
Reset to default 3Check the below working code...
const newData = [];
realData.forEach((val) => {
let obj = {};
val.forEach(v => obj[v.key] = v.value);
newData.push(obj);
});
console.log(newData);
You could map the new objects with Object.assign
for a single object.
var data = [[{ key: "flightNumber", label: "Flight Number", value: "EK 131", "value-type": "String", order: 1 }, { key: "flightId", label: "flightId", value: "1223434", "value-type": "String", order: 2, isId: true }, { key: "CallSign", label: "callSign", value: "123", "value-type": "String", order: 3 }]],
result = data.map(a => Object.assign(...a.map(({ key, value }) => ({ [key]: value }))));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can reduce
each array key/value pair into an object :
const arrays = [
[
{
"key" : "flightNumber" ,
"label": "Flight Number",
"value": "EK 131",
"value-type": "String",
"order" : 1
},
{
"key" : "flightId",
"label": "flightId",
"value": "1223434",
"value-type": "String",
"order" : 2,
"isId": true
},
{
"key": "CallSign",
"label": "callSign",
"value": "123",
"value-type": "String",
"order" : 3
}
]
];
const objects = arrays.map(arr => arr.reduce((acc, cur, i) => {
acc[cur.key] = cur.value;
return acc;
}, {}));
console.log(objects);