-1

It's always tricky to think Array.concat thing. Often, I just want to use mutable Array.push because I simply add extra-data on the immutable data. So, I usually do:

array[array.length] = newData;

I've asked a question related got some answers here: How to store data of a functional chain

const L = (a) => {
  const m = a => (m.list ? m.list : m.list = [])
        .push(a) && m; 
  //use `concat` and refactor needed instead of `push` 
  //that is not immutable
  return m(a); // Object construction!
};

console.log(L);
console.log(L(2));
console.log(L(1)(2)(3))

some outputs:

{ [Function: m] list: [ 2 ] } 
{ [Function: m] list: [ 1, 2, 3 ] }

I feel that push should be replaced with using concat, but still, push makes the code elegant simply because we don't want to prepare another object here.

Basically, I want to do:

arr1 = arr1.concat(arr2);

but, is there any way to write

arr1[arr1.length] = arr2;

which ends up with a nested array, and does not work.

Ivan
  • 34,531
  • 8
  • 55
  • 100

3 Answers3

1

You could assign a new array with a default array for not given m.list.

const L = (a) => {
        const m = a => (m.list = (m.list || []).concat(a), m);
        return m(a);
    };

console.log(L.list);
console.log(L(2).list);
console.log(L(1)(2)(3).list);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thanks, but this is still Destructive assignment which I usually never do. –  Jul 11 '18 at 11:41
  • please add what you want. as i understood the question and some of the comments, you would like to avoid `push`, you want to return an array and you do not like to reassign the array to `list`. why would you like to take a method which returns an array? – Nina Scholz Jul 11 '18 at 11:45
  • Please read the last 3 lines. Thanks, and I'm doing functional programming, and do not use self-mutating function that is `push`. –  Jul 11 '18 at 11:48
0

arr1[arr1.length] represents a single value, the value at index arr1.length.

Imagine this array

[ 1 , 2 , 3 , 4 ]  // arr of length 4
  ^0  ^1  ^2  ^3   // indexes

If we say arr1[arr1.length] = someThing

We ask javascript to put something right here, and only here, at index 4:

[ 1 , 2 , 3 , 4 ,   ] // arr of length 4
  ^0  ^1  ^2  ^3  ^4  // add someThing in index 4

So, if we want to add something strictly with arr1[arr1.length], then we need to keep doing that for each index. for each meaning any kind of loop. E.G:

// Not recommended to use
var arr1 = [1,2,3];
var arr2 = [3,4,5];

while (arr2.length){
  arr1[arr1.length] = arr2.shift();
}

console.log(arr1); // [1,2,3,3,4,5]
console.log(arr2); // []

But, as you can see, this method, or any similar one, even if optimized, is not the right approach. You need a concatenation.

Since you mention a functional one, which returns the resulting array, you can simply replace the initial array and make use of spread operator:

var arr1 = [1,2,3];
var arr2 = [3,4,5];
console.log(arr1 = [...arr1,...arr2]); // [1,2,3,3,4,5]
Adelin
  • 7,809
  • 5
  • 37
  • 65
0

You can use multiple parameters in Array.push so:

var a = [];
a.push(3, 4) // a is now [3, 4]

Combined with the ES6 spread syntax:

var a = [1, 2];
var b = [3, 4]
a.push(...b); // a is now [1, 2, 3, 4] 
Ivan
  • 34,531
  • 8
  • 55
  • 100
Axnyff
  • 9,213
  • 4
  • 33
  • 37