dell
dell

Reputation: 191

Can we multiply each array value between 2 arrays without using loop command in GAS?

I found this one line of code by @Abdennour TOUMI Multiplication 2 arrays javascript/jquery but when I tried it the log only shows each value as the text "Object" only. Did I miss anything? Or there just no other way to do this other than using loop? (assuming the array values for both would always be same length)

function multiplier(){
 let arr = [1, 2, 3, 4, 5];
 let arr1 = [11, 12, 13, 14, 15];
 let multArray = [];

multArray=arr.map(function(e,i){return {value : (e.value * arr1[i].value)}; });
console.log(multArray); // log shows this => [object Object],[object Object],[object Object],[object Object],[object Object]
}

Upvotes: 0

Views: 123

Answers (1)

function multiplier(){
 let arr = [1, 2, 3, 4, 5]
 let arr1 = [11, 12, 13, 14, 15, 22]
 let multArray = []

// The ".value" is not part of javascript I'd use Number() to convert to numbers if needed. 
// Like so: Number(arr1[i]) for exemple.

multArray = arr.map((e,i) => {return {value : e * arr1[i]}})
console.log(multArray) // log shows this now =>  [{value: 11},{value: 24}, {...}] 
}

Upvotes: 3

Related Questions