Shorter / better response to array "trimming" in Javascript?

I came across the question of trimming the first 0 and last 0s of an array (the whole element is 0-9), for example

  • for input [0, 1, 0, 2, 0], the output must be[1, 0, 2]

  • for input [1, 0, 2], the output must be[1, 0, 2]

  • for input [0, 1, 0, 2, 0, 0], the output must be[1, 0, 2]

The basic idea is to find the index of the first nonzero number and the last index of the nonzero index and splicing the original array.

And my way is to change the array to a string and trim it, and then go back to the array again. trimArray=A=>A.join('').replace(/(^['0']*)|(['0']*$)/g, '').split('').map(a=>a-'0')

Are there any other ideas for this?

+4
source share
5 answers

We can do this simply by using array methods ...

var a = [0, 0, 1, 0, 2, 0, 0];
while(a[0] === 0) {
    a.shift();
}

while(a[a.length - 1] === 0) {
    a.pop();
}
console.log(a)

If you need to keep the original array intact :( https://jsfiddle.net/4q0un1kp/ )

function trimZeros(arr)
{
    var result = [...arr];
    while(result[0] === 0) {
        result.shift();
    }

    while(result[result.length - 1] === 0) {
        result.pop();
    }
    return result;
}

var a =  [0, 0, 1, 0, 2, 0, 0];
var b = trimZeros(a);

alert(a);
alert(b);
+8

, , a slice .

O (n) invocations.

function trimZeros (array) {
  var front, back, length = array.length;

  if (!length) return [];

  front = 0;
  back = length - 1;

  while (array[front] === 0) front++

  if (front === length) return [];

  while (array[back] === 0) back--;

  return array.slice(front, back + 1);
}

console.log(trimZeros([0, 1, 0, 2, 0, 0]))
Hide result

, , . ( undefined).

if (!Array.prototype.trim) {
  Array.prototype.trim = function (test) {
    var start, end, length = this.length;

    if (!length) return [];

    start = 0;
    end = length - 1;

    while (start < length && test(this[start], start)) start++;

    if (start === end) return [];

    while (end >= 0 && test(this[end], end)) end--;

    return this.slice(start, end + 1);
  }
}

console.log([0, 0, 1, 0, 2, 0, 0, 0].trim(e => e === 0));
Hide result
+1

var array = [0,0,1,2,0,5,0,0,0,0];

// function to remove all zeros
function removeZeros(array){

  if(array[0] === 0 && array[array.length-1] === 0){
    return removeZeros(array.slice(1,array.length-1));
  }
  else if(array[0] === 0){
    array.shift();
    return removeZeros(array);
  }
  else if(array[array.length-1] === 0){
    array.pop();
    return removeZeros(array);
  }
  else{
    return array;
  }
}

console.log(removeZeros(array)); //[1, 2, 0, 5]

,

0

for:

function trimArray(arr) {
  var lastIndex = arr.length - 1;
  var low = {
      found: false,
      index: 0
    },
    high = {
      found: false,
      index: arr.length
    };

  for (var i = 0; i < arr.length; i++) {
    if (!low.found && arr[i] !== 0) {
      low.index = i;
      low.found = true;
    }
    if (!high.found && arr[lastIndex - i] !== 0) {
      high.index = (lastIndex - i) + 1;
      high.found = true;
    }
    if (high.found && low.found) break;
  }

  if (high.found && low.found) {
    var highCut = -(arr.length - high.index)
    return arr.slice(low.index, highCut ? highCut : arr.length);
  } else {
    return [];
  }
}

var testCases = [
    [0],
    [0, 0],
    [0, 1],
    [0, 1, 0],
    [0, 1, 0, 1, 0],
    [0, 1, 0, 1],
    [1, 0, 1, 0],
    [1, 0, 1]
  ];

var result = testCases.map(trimArray);
results.innerHTML = JSON.stringify(result, null);
<pre id="results"></pre>
Hide result
0
var arr = [0, 0, 2, 3, 0, 0];

while (arr[0] === 0) {
    arr.shift();
}
while (arr[arr.length-1] === 0) {
    arr.pop();
}
-2

Source: https://habr.com/ru/post/1630084/


All Articles