---
# System prepended metadata

title: JavaScript Array Method

---

# JavaScript Array Method
:::warning
:notebook_with_decorative_cover: **Outline**
[toc]
:::
```
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"]
	},

];
```
## forEach()
重新創造一個array，並在每個username後面加上"!"
```
let newArray = []
array.forEach(user => {
	let { username } = user;
	username = username + "!";
	newArray.push(username);
})
console.log(newArray,newArray);
```
output:
```
newArray [ 'john!', 'becky!', 'susy!', 'tyson!' ]
```
## map()
重新創造一個array，並在每個username後面加上"?"
```
const mapArray = array.map(user => {
	let { username } = user;
	return username + "?";
})
console.log("mapArray",mapArray);
```
output:
```
mapArray [ 'john?', 'becky?', 'susy?', 'tyson?' ]
```
## filter()
```
const filterArray = array.filter(user => {
	return user.team === "red";
})
console.log(filterArray);
```
output:
```
[
  {
    username: 'john',
    team: 'red',
    score: 5,
    items: [ 'ball', 'book', 'pen' ]
  },
  {
    username: 'susy',
    team: 'red',
    score: 55,
    items: [ 'ball', 'eraser', 'pen' ]
  }
]
```
## reduce()
```
const total = array.reduce((acc, user) => {
	return acc + user.score
}, 0);
console.log("total",total);
```
output:
```
total 71
```