0

I need to group array items, if they are the same. But only, if they follow after another.

Single items without same value before or after should be ignored.

Example:

    const array = [
        {country : 'usa', continent: 'north america'},
        {country : 'germany', continent: 'europe'},  
        {country : 'france', continent: 'europe'},
        {country : 'india', continent: 'asia'},
        {country : 'netherlands', continent: 'europe'},
       ]

The result should be:

    [
        {country : 'usa', continent: 'north america'},
        [
            {country : 'germany', continent: 'europe'},  
            {country : 'france', continent: 'europe'} 
        ],
        {country : 'india', continent: 'asia'},
        {country : 'netherlands', continent: 'europe'},
       ]
    ]

This solution also would work:

    [
        {country : 'usa', continent: 'north america'},
        {grouped: 'continent', countries: [
            {country : 'germany', continent: 'europe'},  
            {country : 'france', continent: 'europe'} 
            ]
        },
        {country : 'india', continent: 'asia'},
        {country : 'netherlands', continent: 'europe'},
       ]
    ]

3 Answers 3

1

One method would be to loop through the array and store last continent value, if it's the same as previous item, then move previous item into an array and add current item into it.

Array.reduce() could be used for this task:

const array = [
          {country : 'usa', continent: 'north america'},
          {country : 'germany', continent: 'europe'},  
          {country : 'france', continent: 'europe'},
          {country : 'india', continent: 'asia'},
          {country : 'netherlands', continent: 'europe'},
      ];

const newArray = array.reduce((map, item) =>
{
  let prevItem = map[map.length-1]; //get prevous item
  // is previous continent matches current?
  if (prevItem && (prevItem.continent || (prevItem[0] && prevItem[0].continent)) === item.continent)
  {
    if (prevItem.continent) //if it's not an array, convert it into one
    {
      prevItem = [prevItem]; //convert prevous item into array
      map.splice(-1, 1, prevItem); //replace prevous item with new array'ed item
    }
    prevItem[prevItem.length] = item; //append current item to the array
  }
  else
    map[map.length] = item;

  return map;
}, [] /* "map" array */);

console.log(newArray);

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

Comments

1

This should do it.

There is a blank new array which stores the answer.

The algorithm just iterates through the existing array saving a temporary array of matches. When the next entry does not match the previous, it adds that temporary array to a new array.

const array = [{
    country: 'usa',
    continent: 'north america'
  },
  {
    country: 'germany',
    continent: 'europe'
  },
  {
    country: 'france',
    continent: 'europe'
  },
  {
    country: 'india',
    continent: 'asia'
  },
  {
    country: 'netherlands',
    continent: 'europe'
  },
]

const newArray = []
var tempArray = []
var previousContinent = ""

array.forEach(item => {
  if (item.continent === previousContinent) {
    tempArray.push(item)
  } else {

    // Delete this first check if you don't mind every entry being an array
    if (tempArray.length === 1) {
      newArray.push(tempArray[0])
    } else if (tempArray.length > 0) {
      newArray.push(tempArray)
    }
    previousContinent = item.continent
    tempArray = [item]
  }
})

if (tempArray.length === 1) {
  newArray.push(tempArray[0])
} else if (tempArray.length > 0) {
  newArray.push(tempArray)
}

console.log(newArray)

2 Comments

What happened to netherlands ?
Ah good catch! I didn't push the last one. Fixed.
1

Since you have the condition for two countries to be grouped in the same continent they should be adjacent siblings, the quickest way to produce that result and visit the input array only once, is keeping track of the previous continent and fill a queue of countries to push in the output all at once when the next input country breaks the chain.

This demo will output on console the result array crafted as described before:

const array = [
  {country : 'usa', continent: 'north america'},
  {country : 'germany', continent: 'europe'},  
  {country : 'france', continent: 'europe'},
  {country : 'india', continent: 'asia'},
  {country : 'netherlands', continent: 'europe'},
];

function flushQueue(queue, destination, useWrapper = false){
  //if the queue has more than one country
  if(queue.length > 1){
    //if useWrapper was passed to the function as true
    if (useWrapper){
      //overwrites queue with the new wrapper object
      queue = {
        //the continent property is picked by the first item in the queue
        //there's no need to check any other coming next, because anyway 
        //they would be sharing all the same value. The first item will
        //exist for sure because this branch runs if the length>1.
        grouped : queue[0].continent,
        countries : queue
      };
    }
    //adds the whole array queue to the result
    destination.push(queue);
  //otherwise if there's one country only
  }else{
    //adds the single country to the result
    destination.push(queue[0]);
  }
}

function groupCountries(array, useWrapper = false){

  let result = [];
  let prevContinent;
  let queue = [];
  array.forEach((o, i)=>{
    //if this isn't the first element and 
    //the previous continent is different from the current one
    if(typeof prevContinent !== "undefined" && prevContinent != o.continent){
      //flushes the queue of grouped countries to the result array
      flushQueue(queue, result, useWrapper);
      //resets the queue
      queue = [];
    }
    //adds the current country to the queue before it gets flushed in groups
    queue.push(o);
    //refresh prevContinent with the current value before turning to the next
    prevContinent = o.continent;
  });

  //flush the remaining elements in the queue, before..
  flushQueue(queue, result, useWrapper);
    
  //..returning the result
  return result;
}

let result;

//first fashion
result = groupCountries(array);
console.log(result);
//second fashion
result = groupCountries(array, true);
console.log(result);

3 Comments

You've lost netherlands though
Yea I did a terrible mistake and I wasn’t checking if the array queueToFlush was empty before returning the result. there’s the chance like in the Neatherlands case when there’s no next item having the chance to break the chain so it never triggers the flushing. To correct that I had to repeat the push logic there making it a very inelegant approach. It was better factored in a standalone function. I hope you found your solution btw
I took the time to refactor my solution and now it looks great

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.