- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compress array to group consecutive elements JavaScript
We are given a string that contains some repeating words separated by dash (-) like this −
const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-mondaymonday';
Now our job is to write a function that returns an array of objects, where each object contains two properties value and count, value is the word in the string (Monday, Tuesday, Sunday) and count is their consecutive appearance count.
Like for the above string, this array would look something like this −
const arr = [{ val: 'monday', count: 1 }, { val: 'sunday', count: 1 }, { val: 'tuesday', count: 2 }, { val: 'sunday', count: 2 }, { val: 'monday', count: 3 }]
Because monday appears once, then sunday once, tuesday twice, sunday twice and lastly monday thrice.
We will split the array and then use Array.prototype.reduce() method to recursively return the desired array like this −
Here’s the complete code −
Example
const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-mondaymonday'; const str2 = 'friday-friday-sunday-tuesday-sunday-sunday-monday-thursdaymonday'; const compressString = (str) => { return str.split('-').reduce((acc, val) => { const { length: l } = acc; if(acc[l-1]?.val === val){ acc[l-1].count++; return acc; }else{ return acc.concat({ val, count: 1 }); } }, []); } console.log(compressString(str)); console.log(compressString(str2));
Output
The output in the console for the above code will be −
[ { val: 'monday', count: 1 }, { val: 'sunday', count: 1 }, { val: 'tuesday', count: 2 }, { val: 'sunday', count: 2 }, { val: 'monday', count: 3 } ] [ { val: 'friday', count: 2 }, { val: 'sunday', count: 1 }, { val: 'tuesday', count: 1 }, { val: 'sunday', count: 2 }, { val: 'monday', count: 1 }, { val: 'thursday', count: 1 }, { val: 'monday', count: 1 } ]
- Related Articles
- Consecutive elements sum array in JavaScript
- Python – Group Consecutive elements by Sign
- Maximum sum of n consecutive elements of array in JavaScript
- Find consecutive elements average JavaScript
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Check if three consecutive elements in an array is identical in JavaScript
- JavaScript to check consecutive numbers in array?
- Return the sum of two consecutive elements from the original array in JavaScript
- Group a JavaScript Array
- Get average of every group of n elements in an array JavaScript
- C++ program to replace an element makes array elements consecutive
- Check if array elements are consecutive in Python
- Can array form consecutive sequence - JavaScript
- Find elements of array using XOR of consecutive elements in C++
- Add two consecutive elements from the original array and display the result in a new array with JavaScript

Advertisements