0

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]
}
1
  • What is the difference between object and array? Commented Oct 12, 2020 at 15:25

1 Answer 1

3
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}, {...}] 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Answers with no explanation at all are rarely useful to future readers. Consider providing an explanation to help others :)
Thanks, @Raphaël Burkhardt. your edited answer did the job. and when I saw your log result in the comment next to the console.log command, I take it as that word 'value' was actually just a text, so I dropped it and also dropped the curley brackets inside because basically I just need the multiplied values in the 3rd array. multArray=arr.map(function(e,i){return e * arr1[i]; }); got what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.