簡體   English   中英

如何將元素添加到數組但將每個元素放在 Javascript 中的單獨行中

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

我有一個數組問題。 當我使用.push將元素放入數組時,它們會一個接一個地打印在 console.log 中。 [https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/push] 有沒有辦法將 output 每個元素添加到這個數組中的額外行? 我有一個默認值,我應該記錄錯誤。 這些錯誤將是 output 陣列中的控制,每條錯誤消息一行。

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

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

我的意思是這樣嗎?

console.log(sports)       

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

你可以使用循環

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM