Answers for "push two values in array javascript"

1

how to push two values in array at once

let arr = [];
arr.push(1);
arr.push(2);
console.log(arr);

result [1, 2]
Posted by: Guest on June-01-2021
1

how to add multiple elements to A new array javascript

let vegetables = ['parsnip', 'potato']
let moreVegs = ['celery', 'beetroot']

// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot')
Array.prototype.push.apply(vegetables, moreVegs)

console.log(vegetables)  // ['parsnip', 'potato', 'celery', 'beetroot']
Posted by: Guest on November-02-2020
0

sum of two array in javascript

function sumArrays(...arrays) {
  const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
  const result = Array.from({ length: n });
  return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}

console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4
Posted by: Guest on May-03-2020

Code answers related to "push two values in array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language