How to replace elements in array with elements of another array in JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To replace elements with elements of another array, use splice(). Following is the code −

Example

var values = ['John', 'David', 'Bob', 'Mike', 'Sam', 'Carol', 'Adam'];
var marks = [55, 78, 90];
Array.prototype.splice.apply(values, [0, marks.length].concat(marks));
console.log(values);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo303.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo303.js
[ 55, 78, 90, 'Mike', 'Sam', 'Carol', 'Adam' ]
raja
Published on 24-Oct-2020 15:46:19
Advertisements