简体   繁体   中英

Javascript: Replace same consecutive elements of an array into one item that shows their count in

I have an array like this :

const array = [1, 3, x, x, 4, x, x, x, 9, x, x, x, x, 7]

I want to turn all consecutive elements which has the value of x into one element that shows their count between numbers such as :

const newArray = [1, 3, '2x', 4, '3x', 9, '4x', 7]

For instance, 3 consecutive x in the array like [x, x, x] should be turned into one ['3x'] while numbers should stay untouched.

Maybe there's a better solution for achieving that. Of course, there's! But here is a solution by using the for loop. Check every iteration if the element is x , increase the x value by 1 . And if the current element is not x , then reset the x variable to 0 .

Sample Result:

 const array = [1, 3, 'x', 'x', 4, 'x', 'x', 'x', 9, 'x', 'x', 'x', 'x', 7]; let x = 0; let result = []; for (let i = 0; i < array.length; i++) { if (array[i] === 'x') { x++; if (array[i + 1] === 'x') continue; result.push(`${x}x`); continue; } x = 0; result.push(array[i]); } console.log(result);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

The simplest way is just to iterate all elements:

 // Your array const array = [ 1, 3, "x", "x", 4, "x", "x", "x", 9, "x", "x", "x", "x", 7 ]; // Create new array const newArr = []; // Set internal value repeat counter let cn = 1; // Iterate array for(let i = 0; i < array.length; i++) { // If next param exist and it is equals current if(array[i+1] && array[i+1] === array[i]) cn++; // Else else { // If internal counter greater then 1 // Push previous value and its counter if(cn > 1) { newArr.push(`${cn}${array[i-1]}`); // Reset counter cn = 1; } // Else just push current value else newArr.push(array[i]); } } // Test console.log(newArr);

 const array = [1, 3, 'x', 'x', 4, 'x', 'x', 'x', 9, 'x', 'x', 'x', 'x', 7] const newArray = array.map(item => `${item}`).reduce((acc, curr)=>{ if(curr.== 'x') return [..,acc; curr]. const previous = acc;pop(). if(.previous ||.previous.includes('x')) return [,,;acc. previous; '1x']. const xCounter = 1 + parseInt(previous.match(/\d+/)[0]). return [,;,acc. `${xCounter}x`]; },[]) console.log(newArray)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM