2

can multidimensions array return in function to display the all value in the 2dimentions array ??

function display(arr) {


}

const result = display([
["str", 1],
["str2", 2],
["str3", 3]
])

const result2 = display([
["str", 1],
["str2", 2],
["str3", 3],
["str4", 4]
])

const result3 = display([
["str", 1],
["str2", 2]
])

console.log(result)
console.log(result2)
console.log(result3)

i know console.log might work, how about return it in function ?

the output i want it // str = 1, str2 = 2, str3 = 3 // str = 1, str2 = 2, str3 = 3 , str4 = 4 // str = 1, str2 = 2

6
  • 4
    return arr.map(([str, value]) => str + " = " + value).join(", "); Commented Dec 16, 2018 at 15:33
  • return arr.map((v) => v[1]); Commented Dec 16, 2018 at 15:33
  • 1
    BTW, you should let display handle the logging itself, so instead of returning a string that you need to log yourself later, just call console.log directly inside display. Unless of course you want to use the resulting string elsewhere. Commented Dec 16, 2018 at 15:38
  • Possible duplicate of How to print the following Multi-Dimensional array in Java Script? Commented Dec 16, 2018 at 15:45
  • 1
    @ibrahimmahrir your answer it correct Commented Dec 16, 2018 at 15:50

1 Answer 1

0

This work for your code :

function display(arr) {
    return arr.map((key) => `${key[0]} = ${key[1]}`)
}

const result = display([
["str", 1],
["str2", 2],
["str3", 3]
]).join(', ')

// result = "str = 1, str2 = 2, str3 = 3"
Sign up to request clarification or add additional context in comments.

1 Comment

@ZumDummi Done.

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.