How can I mimic Python's step argument in JavaScript?

Question:

In Python, I can overwrite elements in an array using slicing syntax and passing a step argument.
For example:

alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
alist[::2] = ["foo", "foo", "foo", "foo", "foo"]
print(alist) # ['foo', 2, 'foo', 4, 'foo', 6, 'foo', 8, 'foo', 10]

Is there a way to mimic this behavior in JavaScript?

Asked By: jws1

||

Answers:

You can implement such a function yourself with a for loop.

var alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var alist2 = ["foo", "foo", "foo", "foo", "foo"]
function overwrite(arr, arr2, step){
  for(let i = 0, j = 0; i < arr.length && j < arr2.length; i += step, j++){
    arr[i] = arr2[j];
  }
}
overwrite(alist, alist2, 2);
console.log(alist);

Answered By: Unmitigated

You can use map and return an element from the second list on every even iteration.

const alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const blist = ["foo", "foo", "foo", "foo", "foo"];
const clist = alist.map((e,i) => i%2==0?blist[i/2]:e);

console.log (clist)

Answered By: Moritz Roessler

Let me know if this works for you:

function range(from, to){
  let f = from, t = to;
  if(t === undefined){
    t = from; f = 0;
  }
  for(var i=f,r=[]; i<=t; i++){
    r.push(i);
  }
  return r;
}
function step(a, num, b){
  const r = b.slice();
  for(let i=num-1,l=a.length; i<l; i+=num){
    if(!a.hasOwnProperty(i)){
      break;
    }
    r.splice(i, 0, a[i]);
  }
  return r;
}
const aList = range(1, 10), bList = ['foo', 'bar', 'foo', 'bar', 'foo', 'bar'];
console.log(step(aList, 2, bList)); console.log(step(aList, 3, bList));

Answered By: StackSlave

You could implement this with a for..of loop. If you like, you could even define it as a method on Array.prototype, with a signature that looks a bit like the one of splice:

Array.prototype.stepAssign = function (start, step, ...values) {
    for (const value of values) {
        this[start] = value;
        start += step;
    }
}
// Example use:
const alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const alist2 = ["foo", "foo", "foo", "foo", "foo"];
alist.stepAssign(0, 2, ...alist2);
console.log(alist);

In line with JavaScript’s support for sparse arrays, this allows to step beyond the original length of the target array, and so there is no need for an end argument — that end is completely determined by the number of values that are provided.

Answered By: trincot
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.