JavaScript - String.split() but for Arrays?

javascript string to array
javascript split regex
javascript split string by comma
javascript split array
javascript split string at index
javascript string slice
string split java
jquery split string to array

Let's say I have this array of strings (they're HTML elements, but we can use strings to keep it simple):

["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"]

I need a quick way to split this array up by "d". Kinda like String.split(), except for arrays. The end result should be something like this:

[["something", "else", "and"], ["more", "things", "in", "the"], ["array", "etc"]]

Are there any simple one-liners for this? Maybe there's a function built into JS and I'm just missing it?

Well if it's a one-liner you want, here you go:

var myArray = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"];

const result = myArray.reduce((a, c) => c === "d" ? (a.arr[++a.i] = []) && a : a.arr[a.i].push(c) && a, {arr: [[]], i: 0}).arr;

console.log(result);

String.prototype.split(), str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument​. The split () method divides a String into an ordered set of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call. JavaScript Demo: String.split ()

One option would be to join by spaces, then split by ' d ', then split each subarray by spaces:

const input = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"];
const output = input
  .join(' ')
  .split(' d ')
  .map(str => str.split(' '));
console.log(output);

JavaScript, JavaScript's string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or regular  Browse other questions tagged javascript arrays string split or ask your own question. The Overflow Blog The Overflow #25: New tools for new times

let myArray = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"];
let splitArray = [],
    tempArray = [];
myArray.forEach((ele, index) => {
    if(ele !== 'd') {
      tempArray.push(ele);
    }
    if(ele === 'd' || index === myArray.length - 1) {
      splitArray.push(tempArray);
      tempArray = []; 
    }
})

console.log(': ', splitArray);

Convert String to Array with JavaScript's String split Method, The split() method is used to split a string into an array of substrings, and returns I had a similar issue, but more complex as I needed to transform a csv into an  If not used, all elements from the start to the end of array will be included in selection. Negative numbers are used to select from the end. Return value: Returns a new Array, having the selected items. Example-1:This example uses the slice() method to split the array into chunks of array. This method can be used repeatedly to split array of

A simple forEach approach is enough.

var arr = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"];
var result = [], temp = [];

arr.forEach(function(elem, index){
    elem !=='d' ? temp.push(elem) : (result.push(temp), temp = []);
    index==arr.length-1 && (result.push(temp));
});

console.log(result)

How to convert a comma separated string to an array?, Splice, Slice, Split … they all start with S … Splice and Slice are almost twin of pizza starting from second piece to third piece, but excluding the third one. The split() method splits a String object into an array of strings by  The split function splits string objects into numerous objects that are then stored as various elements of an array object. So before I explain how the split function works, I will quickly cover how JavaScript works with strings and also the basics of what arrays are in JavaScript.

To answer your question, there aren't any concise one-liners that come to mind, but you may accomplish what you want with just a few lines of code by iterating over your values and if the word isn't 'd' store it; if it is, then create a new array to hold the next non-'d' value:

const words = ["something", "else", "and", "d", "more", "things", "in", "the", "d", "array", "etc"]

let grouped = words.reduce((response,word)=>{
  if (word!=='d')
    response[response.length-1].push(word)
  else
    response[response.length]=[]
  return response
},[[]])

console.log(grouped)

[ JavaScript ] Splice, Slice, Split - Jean Pan, In JavaScript, split() is a string method that is used to split a string into an array of strings using a specified delimiter. Because the split() method is a method of  The split( ) method doesn’t work directly for arrays. However, we can first convert the elements of our array to a string, then we can use the split( ) method. Let’s see how it works. Firstly, we convert our array to a string with toString( ) method: let myString = array.toString();

JavaScript: String split() method, You can split up strings of text with the Javascript split method. In between When you split a string you turn it into an array. Let's look But look at the third line:. Split() Method JavaScript The JavaScript string splitting method returns an array of substrings obtained by splitting a string on the divider you specify. The separator can be a string or a regular expression.

Javascript Strings - Split and Join, The challenge asks that you split each string into words, but not only by space. For instance. Hello-World // ["Hello", "World"]. For example you can use a regex  In JavaScript, the reverse() method exists only for arrays, so first we need to use split() to transform the string into an array. Then apply the reverse() method and finally join() it all back

Split a String into an Array Using the split Method - JavaScript, The split() method splits a String object into an array of strings by separating the The split() method still splits on every match of separator , but it truncates the  Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:

Comments
  • Yep that's a one-liner ;) heh. I am guessing OP was looking for a native method (different version of split) or method for binning for arrays, given a delimiter.
  • @vol7ron Ye I think so as well. Though unfortunetely there isn't, so something like this will have to do if you want a one-liner haha
  • Nice solution. I hope OP doesn't intend for there to be spaces in any of the strings... :)
  • Gave a one-liner solution for this. It's unreadable like crazy, but it works, lol
  • It a little funky if the array starts or end in "d".
  • Not really, it works the same as string split as ',abc'.split(',') will give ['', 'abc'] so I am happy with it giving an empty array if the array starts with the split value.
  • Honestly, OP should remove the accepted mark from my answer and make this one the accepted answer. It's technically a re-usable one-liner that's much, much more readable as well! +1