简体   繁体   中英

How can add elements to an array but put each element in a separate row in Javascript

I have an array question. When I use .push to put the elements in the array, they are printed in the console.log one after the other. [https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/push] Is there a way to output each element to an extra row in this array? I have a default where I should log errors. These errors are to be output in an array for control with one line per error message.

let sports = ['soccer', 'baseball']
let total = sports.push('football', 'swimming')

console.log(sports)  // ['soccer', 'baseball', 'football', 'swimming']

I mean like this?

console.log(sports)       

['soccer', // line 1
 'baseball', // line 2
 'football', // line 3
 'swimming'] // line 4

u can use a loop

 let sports = ['soccer', 'baseball'] let total = sports.push('football', 'swimming') //table console.table(sports) // loop sports.forEach(element => { console.log(element); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM