0

How would I take the following array in JavaScript

locationList = [{
  id: 1,
  title: "Loughborough"
}, {
  id: 5,
  title: "Corby"
}, {
  id: 2,
  title: "Derby"
}, {
  id: 2,
  title: "Derby"
}, {
  id: 2,
  title: "Derby"
}];

and convert it into something like this:

locationList = [{
  id: 1
  title: "Loughborough",
  count: 1
}, {
  id: 5
  title: "Corby",
  count: 1
}, {
  id: 2
  title: "Derby",
  count: 3
}];

wherein all the titles are totalled up.

5
  • 2
    and what you tried? It sounds like a homework for us :) Commented Jan 22, 2016 at 11:07
  • 2
    create new array, loop through each element, if element.title is not in array, add to array, if it is and to count in specific index. Would have thought it would have been easy with a rep like yours :/ Commented Jan 22, 2016 at 11:11
  • I've been looking at the array.reduce function, but I just wanted another opinion Commented Jan 22, 2016 at 11:11
  • 1
    @thisOneGuy, thanks I will have ago Commented Jan 22, 2016 at 11:12
  • 1
    @daremachine who cares, just write the answer and enjoy the upvotes. Commented Jan 22, 2016 at 12:17

3 Answers 3

4

One of many solutions:

var newl = [];
locationList.forEach(function(o) {
  if (newl[o.id] == undefined) {
    o.count = 1;
    newl[o.id] = o;
  } else {
    newl[o.id].count += 1;
  }
});

// if you want a trimmed array (with length = 3)
var trimedArray = newl.filter(function(n){ return n != undefined }); 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I tried this code, it works very well so I have selected it as the correct answer. However, at the end it tells me the length of my array is 6, when it should be 3. Do you know why this is happening? I think it is creating some empty array elements in the process.
This is because id is actually an integer. The js runtime thinks newl is a regular array... You end up with an array with the biggest number + 1. To trim the array, you cane use: var trimedArray = newl.filter(function(n){ return n != undefined });
Is there any way to remove the empty elements? I am having some rendering issues as a result: imgur.com/quuGXs9
This should create the array correctly: var newl = [],x = -1; locationList.forEach(function(o) { if (newl[o.id] == undefined) { x++; o.count = 1; newl[x] = o; } else { newl[x].count += 1; } });
@gothical: your solution cannot work, since you use the id to check if the element is present, but x to index it...
2

Loop through the elements, create a new array, put an element into the new array if the element is not yet in.

Edit: Otherwise, if it exists, just add 1 to the count in the new array, and then use the new array instead of the old.

Comments

1

As I have said in comments :

Create new array, loop through each element, if element.title is not in new array, add to array, if it is add to count in specific index

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.