我正在嘗試從現有陣列創建一個新陣列并添加“!” 到舊陣列中的每個用戶名。我得到的是['[object Object]!']。
// Complete the below questions using this array:
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
//Create an array using forEach that has all the usernames with a "!" to each of the usernames
const player = []
const newArray = array.forEach((username) => {
player.push(username '!');
})
console.log(player);
uj5u.com熱心網友回復:
慣用的方法是使用Array.prototype.map將一個陣列轉換為另一個陣列。
const array=[{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}];
// using each player, append "!" to the end to create a new array
const players = array.map((user) => user.username "!");
console.log(players);
uj5u.com熱心網友回復:
您需要在播放器推送中添加 username.username,因為現在您獲取的是物件而不是用戶名。
// Complete the below questions using this array:
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
//Create an array using forEach that has all the usernames with a "!" to each of the usernames
const player = []
const newArray = array.forEach((username) => {
player.push(username.username '!');
})
console.log(player);
array.foreach 是從您的陣列中創建一個“物件”,因此每個 {} 都將是一個物件。之后,您需要從物件中呼叫您想要的內容。所以更具可讀性的是這個。
const player = []
const newArray = array.forEach((person) => {
player.push(person.username '!');
})
console.log(player);
uj5u.com熱心網友回復:
干得好。username您試圖將字串添加到物件,而您想在物件的屬性中添加感嘆號
// Complete the below questions using this array:
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
//Create an array using forEach that has all the usernames with a "!" to each of the usernames
const player = []
const newArray = array.forEach(({ username }) => {
player.push(`${username}!`);
})
console.log(player);
參考:
解構
模板字串 (${})
uj5u.com熱心網友回復:
解構輸入forEach()物件:
// {username} wrapping curly brackets around an object key
// pulls that value into the function assigning it to a variable with the same name
const newArray = array.forEach(({username}) => {
player.push(username '!');
})
player輸出:
[
"john!",
"becky!",
"susy!",
"tyson!"
]
forEach()實際上回傳undefined,所以使用:
const newArray = array.forEach((username) => {
...newArray總是undefined,因此毫無意義。簡單使用時forEach():
array.forEach(({ username }) => {
player.push(`${username}!`);
})
更好的是,如果您要創建一個新陣列,請使用map():
const newArray = array.map(({username}) => {
return `${username}!`;
})
console.log(newArray);
還使用模板文字將username變數插入到return'ed 字串中。
// Complete the below questions using this array:
const array = [
{
username: "john",
team: "red",
score: 5,
items: ["ball", "book", "pen"]
},
{
username: "becky",
team: "blue",
score: 10,
items: ["tape", "backpack", "pen"]
},
{
username: "susy",
team: "red",
score: 55,
items: ["ball", "eraser", "pen"]
},
{
username: "tyson",
team: "green",
score: 1,
items: ["book", "pen"]
},
];
//Create an array using forEach that has all the usernames with a "!" to each of the usernames
const newArray = array.map(({username}) => {
return `${username}!`;
})
console.log(newArray);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/526602.html
標籤:javascript数组
