JavaScript How do you merge certain items in a JavaScript array?

Assuming the elements will always be single letters, you can merge the elements, then match on either bs or non-bs:

ab.join('').match(/(b+|.)/g)

const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a'];

let output = ab.join('').match(/(b+|.)/g);

console.log(output);
Answer:1

Using Array#reduce you could do something like this.

I'm assuming the first two characters in your solution were a typo.

const data = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a'];

const res = data
.reduce((a,c)=>{
  const lastIndex = a.length - 1;
  if(a[lastIndex] && a[lastIndex].includes('b') && c === 'b') a[lastIndex] += c;
  else a.push(c);
  return a;
}, []);

console.log(res);
Answer:2

I don't know how to give an explanation for this but using reduce you can do it like this:

const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a'];

function merge(arr) {
  return arr.reduce((acc,cur, index) => {
    if(arr[index - 1] === 'b' && cur !== 'a') {
      acc[acc.length - 1] = acc[acc.length - 1] + cur;
      return acc;
    }
    acc.push(cur);
    return acc;
  },[]);
}

console.log(merge(ab))
Answer:3

Here's what you're after:

var ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a'];

var index = 0;
var groupedArray = [];
var firstOccurence = true;
var groupedString = "";
var grouping = false;
for (var a = 0; a < ab.length; a++) {

  if (ab[a] == "a") {
    if (grouping) {
      grouping = false;
      firstOccurence = true;
      groupedArray.push(groupedString);
    }
    groupedArray.push(ab[a]);
  } else {
    if (firstOccurence) {
      groupedString = "";
      firstOccurence = false;
    }
    groupedString += ab[a];
    grouping = true;
  }
}
console.log(groupedArray);
Answer:4

If you just want to merge b then you could use reduce like this:

If the current item and the previous item are b, then append it to the last accumulator item. Else, push it to the accumulator

const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a']

const output = ab.reduce((acc, c, i, arr) => {
  arr[i-1] === "b" && c === "b" 
    ? acc[acc.length - 1] += c
    : acc.push(c)
    
  return acc;
},[])

console.log(output)
Answer:5

You can just map through the ab array and if the current element is a, push it to a new array but if the current element is b, check if the previous element is b or not, and if it is, merge the current element to the previous element else just push the b to the new array.

const ab = ['a', 'a', 'b', 'b', 'b', 'a', 'b', 'b', 'a'];

let arr = [];

ab.map((e,i) => {
  if(i > 0) { // check if element is the first one or not
    if(e == "b") { 
      if(ab[i - 1].indexOf('b') > -1) { // check if prev element is "b" or not
      	arr[arr.length-1] += e; // merge if prev element is "b"
      } else {
      	arr.push(e);
      }
    } else {
      arr.push(e);
    }
  } else {
  	arr.push(e);
  }
});

console.log(arr);
Answer:6

I have a search bar with a toggle button above it. When I click the toggle button, it gradually changes the opacity. It works great, but it looks a bit clunky, because when I disable it (thus hiding ...

I have a search bar with a toggle button above it. When I click the toggle button, it gradually changes the opacity. It works great, but it looks a bit clunky, because when I disable it (thus hiding ...

I am using angularJS and bootstrap in a single project. When the web page is in the responsive mood , bootstrap navbar menu changes to toggler the menu responsive nav bar. The issue is when I use ...

I am using angularJS and bootstrap in a single project. When the web page is in the responsive mood , bootstrap navbar menu changes to toggler the menu responsive nav bar. The issue is when I use ...

  1. angular with bootstrap 4
  2. angular with bootstrap example
  3. angular with bootstrap tutorial
  4. angular with bootstrap or material
  5. angular with bootstrap 4 tutorial
  6. angular with bootstrap 3
  7. angular with bootstrap theme
  8. angular with bootstrap stackblitz
  9. angular with bootstrap 4 example
  10. angular with bootstrap template
  11. angular with bootstrap
  12. angular with bootstrap modal
  13. angular with bootstrap css
  14. angular with bootstrap table
  15. angular with bootstrap dropdown
  16. angular with bootstrap and jquery
  17. angularjs bootstrap
  18. angular with bootstrap grid
  19. angular with bootstrap datepicker
  20. angular with bootstrap sass

Following this topic, I am trying to generate a 3D curved triangle as a NURBS surface, but I don't understand how to set up my 3D points to do that. Here is the current implementation : var edges = ...

Following this topic, I am trying to generate a 3D curved triangle as a NURBS surface, but I don't understand how to set up my 3D points to do that. Here is the current implementation : var edges = ...

I must be missing something obvious. Turning to SO after a day of frustrated headbanging ;-) : Given that I have created a time domain axis using d3.time.scale and d3.svg.axis, how can I later modify ...

I must be missing something obvious. Turning to SO after a day of frustrated headbanging ;-) : Given that I have created a time domain axis using d3.time.scale and d3.svg.axis, how can I later modify ...

  1. d3 resize axis
  2. d3 responsive axis