Reputation: 61
I simply passing array to firebase to store data but it is not efficient way to query data from array. SO , I am planning to store data in mapping format. But not sure how to do that.
{
"ClassDetails":
[
{
"Code": "CIS695",
"Instructor": "Dr. Antony",
"Location": "BB444",
"Name": "Capstone project"
}
],
"PersonalDetials": [
{
"Email": "[email protected]",
"FName": "Ravi",
"LName": "Shah",
"Major": "MSIS"
}
]
}
Want to change in this format
{
"ClassDetails":
"CIS695"{
"Code": "CIS695",
"Instructor": "Dr. Antony",
"Location": "BB444",
"Name": "Capstone project"
}
"CIS690"{
"Code": "CIS690",
"Instructor": "Dr. Smith",
"Location": "BB444",
"Name": "Project Management"
}
"PersonalDetials":
{
"Email": "[email protected]",
"FName": "Ravi",
"LName": "Shah",
"Major": "MSIS"
}
}
Tried everything but couldn't find the proper solution.Data structure in firebase
Upvotes: 2
Views: 65
Reputation: 39432
Considering that the structure of the data
won't change, give this a try:
const data = {
"ClassDetails": [{
"Code": "CIS695",
"Instructor": "Dr. Antony",
"Location": "BB444",
"Name": "Capstone project"
}],
"PersonalDetials": [{
"Email": "[email protected]",
"FName": "Ravi",
"LName": "Shah",
"Major": "MSIS"
}]
};
const mappedData = { ...data
};
mappedData.ClassDetails = {};
for (const item in data.ClassDetails) {
const itemToAdd = { ...data.ClassDetails[item]
};
mappedData.ClassDetails[itemToAdd.Code] = itemToAdd;
}
mappedData.PersonalDetials = mappedData.PersonalDetials[0];
console.log(mappedData);
Upvotes: 1
Reputation: 22213
Try like this:
output = {};
constructor() {
let classDetails = {};
this.input.ClassDetails.forEach(item => {
classDetails[item.Code] = item;
});
this.output["ClassDetails"] = classDetails;
this.output["PersonalDetials"] = this.input.PersonalDetials[0];
console.log(this.output);
}
Upvotes: 0