2

My intention is to split an array using a separator and choose a limit of its splitting.

var arr = [89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'];
var res = [];
while (arr.length) {
  res.push(arr.splice(0, 2));
}
console.log(res)

For example: If 0 is the separator and 2 is the limit

arr.split(0, 2)
[[89, 'sdf'], [null]] //output
5
  • why is the zero gone? Commented Apr 16, 2019 at 8:30
  • Why do you keep null but remove 0? Commented Apr 16, 2019 at 8:31
  • @NinaScholz this is because he wants to split the array using 0 as delimiter Commented Apr 16, 2019 at 8:31
  • How does this work?? Shouldn't it be [[89, "sdf"], [0, true]]? Commented Apr 16, 2019 at 8:33
  • The idea is to use the 0 as the separator and output to be [[89, 'sdf'], [null]] if user choose to split the array like this arrat.split(0, 2) or array.separate(0, 2). Commented Apr 16, 2019 at 8:35

3 Answers 3

1

You could use indexOf and splice like this:

var arr = [89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'];

function splitArray(arr, seperator, count) {
  let result = [], 
        copy = arr.slice(), // if you don't want to mutate the original array
        counter = 0;
  
  while(counter < count && copy.length > 0) {
    const index = copy.indexOf(seperator);
    if(index !== - 1) {
      result.push(copy.splice(0, index));
      copy.shift(); // remove the seperator
    }
    else result.push(copy.splice(0)) // add the remaining items

    counter++
  }
  
  return result
}

console.log(splitArray(arr, 0, 2))
console.log(splitArray(arr, 0, 3))

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

Comments

1

You could iterate the array and take a counter, which works as index for the result array and push the item into the result array of the counter.

function split(array, length, separator) {
    var result = [],
        count = 0,
        i = 0;

    while (count < length && i < array.length) {
        if (array[i] === separator) {
            count++;
            i++;
            continue;
        }
        result[count] = result[count] || [];
        result[count].push(array[i++]);
    }
    return result;
}

console.log(split([89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'], 2, 0));

Version with indexOf and slice.

function split(array, length, separator) {
    var result = [],
        p,
        i = 0;

    while ((p = array.indexOf(separator, i)) !== -1 && result.length < length) {
        result.push(array.slice(i, p));
        i = p + 1;
    }
    if (result.length < length) result.push(array.slice(i));
    return result;
}

console.log(split([89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'], 2, 0));
console.log(split([89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'], 5, 0));
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Ah smart and simple, I wrote a solution using serialization, split, trim, deserialization, but good old loops will easier do the job
0

you can use this one

var arrays = [], size = 2;

var arr = [89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'];
while (arr.length > 0)
    arrays.push(arr.splice(0, size));

Comments

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.