3

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

7
  • Won't you run into a problem if you have, say title: Twitter and title: Twitch in different objects? Commented Sep 4, 2018 at 14:58
  • what do you mean? @ScottMarcus Commented Sep 4, 2018 at 14:59
  • 3
    It looks like you want to create keys (A, F, T) based on the first letter of each object's title key. Your desired output needs to be an object (you currently have it as an array), but you can't have duplicate keys in an object. Commented Sep 4, 2018 at 15:00
  • @ScottMarcus Exactly, I don't want duplicate keys. Take F for example, I want the F array to contain both objects with titles starting with F Commented Sep 4, 2018 at 15:02
  • 1
    Possible duplicate of What is the most efficient method to groupby on a JavaScript array of objects? Commented Sep 4, 2018 at 15:03

3 Answers 3

1

How about:

var allApps = [
 {
  title: "amazon",
  summary: "lorem ipsum"
 },
 {
  title: "facebook",
  summary: "lorem ipsum"
 },
 {
  title: "twitter",
  summary: "lorem ipsum"
 },
 {
  title: "flipp",
  summary: "lorem ipsum"
 }
]

var d = {};
for (var i=0; i<allApps.length; i++) {
  var l = allApps[i].title.substring(0,1);
  // ensure object has key
  if (!d[l]) d[l] = [];
  d[l].push(allApps[i]);
}

console.log(d)

This logs:

{
  a: [
    { title: 'amazon', summary: 'lorem ipsum' }
  ],
  f:[
    { title: 'facebook', summary: 'lorem ipsum' },
    { title: 'flipp', summary: 'lorem ipsum' }
  ],
  t: [
    { title: 'twitter', summary: 'lorem ipsum' }
  ]
}

The idea here is we just loop over allApps, examine the first letter of the title attribute on the current member of that array, then check if that letter is already a key in our new dictionary, d. If not, we add that letter as a key in d. Then we add the current element from allApps to the list of values for that key. That's it!

Sign up to request clarification or add additional context in comments.

Comments

1

Use the reduce function and from the current object get the first character. This first character is going to be. If this key exist then update it's value

var allApps = [{
    title: "amazon",
    summary: "lorem ipsum"
  },
  {
    title: "facebook",
    summary: "lorem ipsum"
  },
  {
    title: "twitter",
    summary: "lorem ipsum"
  },
  {
    title: "flipp",
    summary: "lorem ipsum"
  }
]
let k = allApps.reduce(function(acc, curr) {
  let getFirstChar = curr.title.charAt(0).toUpperCase();

  if (acc[getFirstChar] === undefined) {
    acc[getFirstChar] = [];
  }
  acc[getFirstChar].push(curr)
  return acc;



}, {})
console.log(k)

1 Comment

This feels a lot easier to read and understand once you know how to use the reduce method.
0

You don't need the variable letters. You should be pushing onto titles[firstletter].

And titles should be initialized as an object, not an array.

var allApps = [
 {
  title: "amazon",
  summary: "lorem ipsum"
 },
 {
  title: "facebook",
  summary: "lorem ipsum"
 },
 {
  title: "twitter",
  summary: "lorem ipsum"
 },
 {
  title: "flipp",
  summary: "lorem ipsum"
 }
];

var titles = {};
for (var i = 0; i < allApps.length; i++) {
  title = allApps[i].title;
  if (title == "") {
    continue;
  }
  var firstLetter = title.substring(0, 1).toUpperCase();
  if (!titles[firstLetter]) {
    titles[firstLetter] = [];
  };
  titles[firstLetter].push(allApps[i]);
}

console.log(titles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.