
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Iterating through an array, adding occurrences of a true in JavaScript
Suppose we have an array of true/false represented by 't'/'f' which we retrieved from some database like this −
const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't'];
We are required to write a JavaScript function that takes in one such array. Our function should count the consecutive appearances of those 't' that are sandwiched between two 'f's and return an array of that count.
Therefore, for the above array, the output should look like −
const output = [1, 3, 6, 1];
Example
The code for this will be −
const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't']; const countClusters = (arr = []) => { let res = []; res = arr.reduce((acc, val) => { const { length: l } = acc; if(val === 't'){ acc[l - 1]++; } else if(acc[l - 1] !== 0){ acc.push(0); }; return acc; }, [0]); return res; }; console.log(countClusters(arr));
Output
And the output in the console will be −
[ 1, 3, 6, 1 ]
- Related Articles
- Iterating through a dictionary in Swift
- Adding an element in an array using Javascript
- Unique number of occurrences of elements in an array in JavaScript
- Remove all occurrences of a multiple occurring element in an array in JavaScript
- Looping through an array in Javascript
- Adding an element at a given position of the array in Javascript
- Find all occurrences of a word in array in JavaScript
- How to count number of occurrences of repeated names in an array - JavaScript?
- Adding an element at the end of the array in Javascript
- Adding an element at the start of the array in Javascript
- Adding two values at a time from an array - JavaScript
- How to check whether an array is a true array in JavaScript?
- Iterating over array in Java
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Loop through an index of an array to search for a certain letter in JavaScript

Advertisements