---
# System prepended metadata

title: 陣列、函式、變數 - 範例程式

---

# 陣列、函式、變數 - 範例程式


#### 新增修改物件陣列

```javascript=1
var arr = ['20191225'];
var obj = {time1: '20191224'};

arr[0] = '20191224';
obj.time2 = '20191225';

console.log(arr, obj);
```

#### 利用 map 產生新的陣列

```javascript=1
var newReleases = [
  {
    "id": 70111470,
    "title": "Die Hard",
    "rating": [4.0],
    "bookmark": []
  },
  {
    "id": 654356453,
    "title": "Bad Boys",
    "rating": [5.0],
    "bookmark": [{ id: 432534, time: 65876586 }]
  }
]

function generateKeyArr(value) {
  let result;
  result = value.map(function(item, index, arr){
	var obj = {
		id: item.id,
		title: item.title
    };
    return obj;
  });
  return result;
}

console.log(generateKeyArr(newReleases))
```

#### 接受不同型別強制轉型的加法

```javascript=1
function plus(number1, number2) {
  return (+number1) + (+number2)
}

console.log(plus(4, 2));
console.log(plus('4', 2));
console.log(plus(4, '2'));
```

#### 更改傳入參數（物件）的函式

```javascript=1
var person = {name: 'chad'};
function modifyObj(obj) {
    obj.name = 'peter';
}

modifyObj(person);
console.log(person);
// {name: 'peter'}

```

#### 更改傳入參數（字串）的函式

```javascript=1
var name = 'chad';
function modifyValue(name) {
    // 無法直接更改成功，此為刻意看見錯誤的範例，詳見下一區塊的重新賦值寫法
}

modifyValue(name);
console.log(name);
// peter
```


更改物件重新 assign 的函式
```javascript=1
var person = {name: 'chad'};
function reassignObj() {
    return {
        name: 'peter'
    };
}

person = reassignObj(person);
console.log(person);

```

