1

So lets say I have this array var:

arr = ["a","b","c","d","a","b","a","b","a"]

and so on the way a will appear is just random I want to divide this array from one a to the next a for example my new array could be like

[["a","b","c","d"],["a","b"],["a","b"],["a"]]
6
  • 8
    What have you tried in order to achieve that? Commented Apr 3, 2019 at 11:06
  • 4
    Why don't you do it? What is bothering you here? You didn't provide any code. :/ Commented Apr 3, 2019 at 11:06
  • 3
    The bare minimum code expected here is a simple for loop and check if the current character is "a". Please read: How much research effort is expected of Stack Overflow users? Commented Apr 3, 2019 at 11:08
  • @Meet Refer this stackoverflow.com/help/how-to-ask Commented Apr 3, 2019 at 11:10
  • i am sorry i didnt share my code , Commented Apr 3, 2019 at 11:30

6 Answers 6

4

You could reduce the array and push a new array to the result if you have the wanted value. Then push all values to the last array in the result set.

var array = ["a", "b", "c", "d", "a", "b", "a", "b", "a"],
    result = array.reduce((r, v, i) => {
        if (!i || v === 'a') r.push([]);
        r[r.length - 1].push(v);
        return r;
    }, []);

console.log(result);

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

4 Comments

I am wondering about what is result if first element of array is not "a" ?
@SudhirOjha that's an invalid scenario since the characters are always sorted alphabetically "from a to the next a"
@SudhirOjha, i added a check for the first item.
@B001ᛦ you suprisingly get much more points by answering easy off-topic questions or blatant duplicate than obscure interesting ones..
1

You can convert array to string and then use match() and RegExp. Then then use map() to get array of arrays

let arr = ["a","b","c","d","a","b","a","b","a"]

let res = arr.join('').match((/a[^a]+/g)).map(x => [...x]);
console.log(res)

Comments

0

You can do following.

First convert array to string:- "abcdababa" (using .join(""))

Now split string with "a" as seprator:- ["","bcd","b","b",""] (using .split("a"))

Now prepend "a" to all the element:- ["a","abcd","ab","ab","a"] (using .map(x=>"a"+x))

Now split all element with ""(empty string) as seprator:- [["a"],["a","b","c","d"],["a","b"],["a","b"],["a"]] (using .split("")) (this step will be inside map function)

Now remove first element(as it is not part of your string):- [["a","b","c","d"],["a","b"],["a","b"],["a"]] (using .shift())

var arr = ["a","b","c","d","a","b","a","b","a"];
var x1 = arr.join("").split("a").map(x => {
  x = "a" + x;
  return x.split("");
});
x1.shift();
console.log(x1);

Comments

0

A succinct method is to join your characters into a string, then split on each a using a positive lookahead regex to keep the a in the split parts, and finally split the resulting parts into arrays:

const arr = ["a","b","c","d","a","b","a","b","a"];

const result = arr.join('').split(/(?=a)/).map(x => x.split(''));

console.log(JSON.stringify(result));

Comments

0

using simple forEach

var arr = ["a", "b", "c", "d", "a", "b", "a", "b", "a"];
var result = [];
var i = -1;

arr.forEach(function(val) {
    if (val == 'a') {
        result.push([]);
        i++;
    }
    result[i].push(val);
})

console.log(result);

Comments

0

You could do this with split()/join() methods like:

let arr = ["a", "b", "c", "d", "a", "b", "a", "b", "a"];
let r = arr.join('').split('a').map(x => (["a", ...x.split('')]));
r.shift();

console.log(r);

2 Comments

The second +a is extra arr.join('').split('a').map(x => (["a",...x.split('')]));. Just add "a" in map()
Thanks for your suggestion @MaheerAli ... Updated.

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.