Reputation: 528
i have a dynamic json like below in the nested json structure
{
"data": [
{
"_score": 0.5753642,
"_type": "data",
"_id": "686",
"_source": {
"mainData": "subset",
"data": "vehicle",
"name": "subset_vehicle",
"fields": [
"number",
"id",
"chasis"
],
"dataset": "chasis"
},
"_index": "vehicle"
},
{
"_score": 0.575,
"_type": "data",
"_id": "687",
"_source": {
"mainData": "subset1",
"data": "vehicle1",
"name": "subset_vehicle1",
"fields": [
"number",
"id",
"chasis"
],
"dataset": "chasis1"
},
"_index": "vehicle"
},
{
"_score": 0.57,
"_type": "data",
"_id": "686",
"_source": {
"mainData": "subset",
"data": "vehicle",
"name": "subset_vehicle",
"fields": [
"number",
"id",
"chasis"
],
"dataset": "chasis"
},
"_index": "vehicle"
}
]
}
and i want to convert it as flat json kind off
{
"_id": "686",
"mainData": "subset",
"data": "vehicle",
"dataset": "chasis"
"name": "subset_vehicle",
"fields": [
"number",
"id",
"chasis"
]
}
so when ever i click on the id on the front end side based on that id particular object has to be converted like the above i mean flat json . i used to below logic to convert
var flatArray = [];
var flatObject = {};
for (var index = 0; index < data.length; index++) {
for (var prop in data[index]) {
var value = data[index][prop];
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
for (var inProp in value[i]) {
flatObject[inProp] = value[i][inProp];
}
}
}else{
flatObject[prop] = value;
}
}
flatArray.push(flatObject);
}
console.log(flatArray);
but i unable to convert it
Upvotes: 2
Views: 376
Reputation: 154
You can simply us Array.flatMap()
or Array.map()
methods to return a new array. As per your requirement, both will work. Array.flatMap() & Array.map()
var data = [{"_score":0.5753642,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"},{"_score":0.575,"_type":"data","_id":"687","_source":{"mainData":"subset1","data":"vehicle1","name":"subset_vehicle1","fields":["number","id","chasis"],"dataset":"chasis1"},"_index":"vehicle"},{"_score":0.57,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"}]
var a = data.flatMap(d => {
d._source._id = d._id;
return d._source;
})
console.log(a)
Upvotes: 5
Reputation: 350242
You can use object spread syntax for that:
const data = [{"_score": 0.5753642,"_type": "data","_id": "686","_source": {"mainData": "subset","data": "vehicle","name": "subset_vehicle","fields": ["number","id","chasis"],"dataset": "chasis"},"_index": "vehicle"},{"_score": 0.575,"_type": "data","_id": "687","_source": {"mainData": "subset1","data": "vehicle1","name": "subset_vehicle1","fields": ["number","id","chasis"],"dataset": "chasis1"},"_index": "vehicle"},{"_score": 0.57,"_type": "data","_id": "686","_source": {"mainData": "subset","data": "vehicle","name": "subset_vehicle","fields": ["number","id","chasis"],"dataset": "chasis"},"_index": "vehicle"}];
const flatArray = data.map(({_id, _source}) => ({ _id, ..._source }));
console.log(flatArray);
Upvotes: 2