Reputation: 41
Iam new to javascript and help is very much appreciated. I have the following array:
let array =
[{key: "sabc", value: 19},
{key: "spalabc", value: 21},
{key: "spalte2", value: 22},
{key: "spabc2", value: 22},
{key: "spabce2", value: 23}]
I want to have a new array, where all the values are changed through a function (in this case logarithm base 10). So the new array should look like this:
let array =
[{key: "sabc", value: 1.278753601},
{key: "spalabc", value: 1.322219295},
{key: "spalte2", value: 1.342422681},
{key: "spabc2", value: 1.342422681},
{key: "spabce2", value: 1.361727836}]
I tried forEach, but i dont get the result i want.
Upvotes: 3
Views: 69
Reputation: 28434
You can use .map
:
let array =
[{key: "sabc", value: 19},
{key: "spalabc", value: 21},
{key: "spalte2", value: 22},
{key: "spabc2", value: 22},
{key: "spabce2", value: 23}]
arr = array.map(e => {
return {...e, value:Math.log10(e['value'])};
});
console.log(...arr);
Upvotes: 2
Reputation: 2761
let array =
[{key: "sabc", value: 19},
{key: "spalabc", value: 21},
{key: "spalte2", value: 22},
{key: "spabc2", value: 22},
{key: "spabce2", value: 23}]
const result = array.map(a => ({ ...a, value: Math.log10(a.value)}))
console.log(result)
Upvotes: 0