Maurip00
Maurip00

Reputation: 39

JavaScript: array of integers, sum integer to each element

noob question... As the title, I would add for example 5 to each element of an array like this

array = [3,6,23,5,43]

Result would be newarray = [8,11,28,10,48]

Maybe with map operator?

Upvotes: 2

Views: 52

Answers (1)

John Humphreys
John Humphreys

Reputation: 39254

You can do a map like this:

var result = array.map(x => x + 5)

Or in more traditional syntax:

var result = array.map(function(x) { 
    return x + 5
});

Upvotes: 3

Related Questions