0

I have three variables a, b and c. I have to insert these into an array called arr. On calling function click, these variables should get pushed into arr array. eg. if i call click with arg 1,2 & 3 then array should be [1,2,3], now when I will pass the args 6,7 then the array should be [1,2,3,6,7]. In the code below, I tried to use concat method to get the desired result but i was unsuccessful.Is there any way around this?

function click(a,b,c){

    var A=a;
    var B=b;
    var C=c;

    var arr=[]
    var newArr=[A,B,C]
    arr.concat(newArr);
    console.log(arr);

}

click(10,11,12);
click(12,13,14);

6
  • you should declare the variable 'arr' outside the function click Commented Mar 22, 2018 at 4:54
  • you can use arr.push(newArr); Its another way to insert into an array. Commented Mar 22, 2018 at 4:56
  • Still didn't work ":( Commented Mar 22, 2018 at 4:56
  • @AlauddinAhmed i am getting [ [ 10, 11, 12 ], [ 12, 13, 14 ] ] ... not [10,11,12,12,13,14] :( Commented Mar 22, 2018 at 4:58
  • you can do arr.push(a, b, c) also, you dont need to assign them to another variable or array. Commented Mar 22, 2018 at 5:01

7 Answers 7

3

Declare a global array outside of the function and then after push values into it.

var arr = [];
function click(a,b,c){
    arr.push(a, b, c)
}

click(10,11,12);
click(12,13,14);

console.log(arr);

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

Comments

3

The problem with your code is that, you are concatenating to the array but not considering the return value.

arr = arr.concat(newArr);

Then declare the array global to hold the values for every click.

Comments

1

function click(arr, a, b, c){
    arr.push(a, b, c);
    console.log(arr);
}

var arr = [];
click(arr, 10, 11, 12);
click(arr, 12, 13, 14);

Comments

1

If you don't want your arr outside your function, another approach is storing your arr inside click scope then call it anywhere (it doesn't pollute your global scope):

    let click = (function (arr) {
       return function (a,b,c) {
    
        var A=a;
        var B=b;
        var C=c;
    
        var newArr=[A,B,C]
        arr = arr.concat(newArr);
        console.log(arr);
      }
    })([])

    click(10,11,12);
    click(12,13,14);

Comments

0

First of all. You should either define arr outside your function or pass arr into function as a parameter. Second one. Function concat returns concatenated arrays and left original arrays unchanged. So you need something like arr=arr.concat(newArr)

Comments

0

Destructuring assignment can be used to functionally concatenate variables with arrays.

See below for a practical example.

// Input.
const input = [1, 2, 3]

// Click.
const click = (array, ...arguments) => [...array, ...arguments]

// Output.
const output = click(input, 4, 5, 6)

// Proof.
console.log(output)

Comments

0

Your Problem here is that the concat function returns a new array instance with the elements: Check Here

    function click(a,b,c){

        var A=a;
        var B=b;
        var C=c;

        var arr=[]
        var newArr=[A,B,C]
        arr = arr.concat(newArr);
        // concat does not change the ref itself
        console.log(arr);

    }

click(10,11,12);
click(12,13,14);

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.