Break an array into smaller arrays, using a specific character as a break point

Related searches

I have an array like this:

  [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ]

I want to break it into subarrays based on where the '-' is, so it would look like this:

  [ [3, 8, 18], [19, 3, 8, 20, 19, 3, 8], [22] ]

I need to write a function to do this for other similar arrays.

I have tried using the slice method but I can't quite figure out how to make it work. Any ideas would be appreciated.

you can iterate over your array and fill a temp-array with the content, when you find your special split-character, you can push the temp array into a result-array, empty the temp-array and then continue the loop.

let data = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ];

let result = [];
let temp = [];

for (let item of data) {
  if (item == '-') {
    temp = [];
  } else {


Array Chunking ―, Array chunking is a technique used to split very large arrays into smaller Given an array and a chunk size, divide the array into many sub-arrays where each the array using a for…of loop as we manually break it down into smaller groups. parameters: the starting index and the number of elements from that point to be � In the challenge sample, we split the array of 13 elements into smaller groups of 5, with the last group having only 3 elements because the elements from the supplied array have been exhausted. In the same manner, we are to write a function that goes through the elements of the array received and split it into groups as specified by the size

I believe this is the answer you are looking for:

l =  [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ];

let res = [];
let subres = [];

l.forEach((n, index) => {
  if (n !== '-') {
  } else {
    subres = [];

  if (index === l.length - 1 && subres.length >= 1) {


Break an array into maximum number of sub-arrays such that their , C++ prpgram to break given array into maximum. // number of subarrays with equal average. If last break point was not end of array, we. Array Break Apart Page 4 (for advanced students) Array Break Apart Page 4, Color (for advanced students) This free PDF worksheet packet is the perfect addition to math centers. It's ideal to complete either alone or in pairs and can initiate great conversations as each array has several different ways in which to break apart.

You can use reduce:

const arr = [3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22];
const res = arr.reduce((acc, curr) => {
  curr == "-" ? acc.push([]) : acc[acc.length - 1].push(curr);
  return acc;
}, [[]]);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Array methods, To make things easier, in this chapter they are split into groups. The arrays are objects, so we can try to use delete : So, special methods should be used. But if an array-like object has a special Symbol. as their string counterparts, but operate on items instead of characters: There's a small catch. PowerShell Performance: += Operator versus ArrayList to add Elements to an Array; PowerShell here-string a handy multiline string value; PowerShell: how to Split an Array into smaller Arrays (Chunks)? Recent Comments. pv2b on Active Directory PowerShell Delegate Permission to Reset User Passwords for a specific Organizational Unit

You can do a function to do this for you and either inside use Array.reduce or Array.forEach to go through each of the elements and "group" based on the location of a passed in character.

var data = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ]

let chunkByChar = (arr, char) => {
  let result = [[]]
  arr.forEach(x => x === char ? result.push([]) : result[result.length-1].push(x))
  return result

console.log(chunkByChar(data, '-'))
console.log(chunkByChar(data, 8))
console.log(chunkByChar(data, 3))

Let's clear up the confusion around the slice( ), splice( ), & split , Firstly, you need to understand how JavaScript arrays work. Like in other programming languages, we use arrays to store multiple data in JS. For example, I want to slice the first three elements from the array The slice( ) method doesn't include the last given element. Each character split one by one � Tried to use an example below (#56022) for array_chunk_fixed that would "partition" or divide an array into a desired number of split lists -- a useful procedure for "chunking" up objects or text items into columns, or partitioning any type of data resource.

how to split array to multiple arrays in javascript Code Example, Get code examples like "how to split array to multiple arrays in javascript" instantly right from your google search results with the Grepper� Functions that accept character arrays (and cell arrays of character vectors) as inputs also accept string arrays. Represent Text with Character Vectors. To store a 1-by-n sequence of characters as a character vector, using the char data type, enclose it in single quotes.

You mean like this Split an array into parts in Powershell This function split an array into desired number of chunks and the chunks are returned as a separate array.

Split (Char [], StringSplitOptions) Splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements. Split (Char [], Int32) Splits a string into a maximum number of substrings based on the characters in an array.

  • The slice() method should work. Get the index of the - element, than use that as the end of the slice.
  • Show how you tried to solve it with slice(). Then we can show where you went wrong and how to fix it. But we're not going to do it from scratch for you.
  • Thank you very much. I was struggling to understand how I could iterate over the array to do this. But I see from your very clear answer that I will be pushing the numbers into temp [] until we get to the '-' and at that point we push the filled temp into result, and then reset the temp to an empty array to continue on down the line. The logic was hard for me to get on my own but hopefully I can learn from this, thank you!!!