I am trying to convert an array of objects that looks in this:
var allApps = [
{
title: "amazon",
summary: "lorem ipsum"
},
{
title: "facebook",
summary: "lorem ipsum"
},
{
title: "twitter",
summary: "lorem ipsum"
},
{
title: "flipp",
summary: "lorem ipsum"
}
]
Into something that looks like this:
var titles= {
A: [
{
title: "amazon",
summary: "lorem ipsum"
}
],
F: [
{
title: "facebook",
summary: "lorem ipsum"
},
{
title: "flipp",
summary: "lorem ipsum"
}
],
T: [
{
title: "twitter",
summary: "lorem ipsum"
}
]
}
So far I have this:
var letters = [];
var titles = [];
for (var i = 0; i < allApps.length; i++) {
title = allApps[i].title;
if (title=="") {
continue;
}
var firstLetter = title.substring(0,1);
var arrayWithFirstLetter = titles[firstLetter];
if (arrayWithFirstLetter == null) {
titles[firstLetter] = [];
letters.push(firstLetter);
};
}
I essentially want to sort the apps based on the title property and push them into the array with the corresponding letter.
Right now my code takes the first letter of each of the titles and creates an array of arrays for each letter