JSメソッド備忘録

#JSメソッドの備忘録
お仕事でよく目にする、というかヘビロテしているメソッドをまとめました。
##map

```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'];

const result = favorite.map( x => x.length);
console.log(result);
// 出力 [ 10, 8, 6, 7 ]
```

##find
条件に一致した値の中で、配列内の一番最初の値が返される。
配列に複数masayasuがあっても、返されるmasayasuは一つだけ。

```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'];
const search1 = 'masayasu';
const result1 = favorite.find( x => x === search1);

console.log(result1);
// 出力 masayasu
```
##findIndex
```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'];

const search2 = 'masayasu';
const result2 = favorite.findIndex(x => x === search2);
console.log(result2);
// 出力 1

const search3 = 'yamasato';
const result3 = favorite.findIndex(x => x === search3);
console.log(result3);
// 出力 -1
```
##indexOf
```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki', 'masayasu'];

const search = 'masayasu';
const result1 = favorite.indexOf(search);
console.log(result1);
// 出力 1

const result2 = favorite.indexOf(search, 2);
console.log(result2);
// 出力 4

const missSearch = 'yamasato';
const result3 = favorite.indexOf(missSearch);
console.log(result3);
// 出力 -1
```

##includes
```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki', 'masayasu'];

const search1 = 'masayasu';
const result1 = favorite.includes(search1);
console.log(result1);
// 出力 true

const search2 = 'yamasato';
const result2 = favorite.includes(search2);
console.log(result2);
// 出力 false
```

##filter
```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'];

const result1 = favorite.filter(x => x.length > 6);
console.log(result1);
// 出力 [ 'wakabayasi', 'masayasu', 'tosiaki' ]

const result2 = favorite.filter(x => x.length < 5);
console.log(result2);
// 出力 []
```
よく使うのは配列内の空の配列か連想配列を除去するとき

```js
const favorite = ['wakabayasi', [], 'masayasu', [], 'kasuga', 'tosiaki'];

const result = favorite.filter( x => x.length);
console.log(result);
// 出力 [ 'wakabayasi', 'masayasu', 'kasuga', 'tosiaki' ]
```

##reduce
```js
const favorite = ['wakabayasi', ' ', 'masayasu', ' & ', 'kasuga', ' ', 'tosiaki'];

const result = favorite.reduce( (x, y) => x + y);
console.log(result);
// 出力 wakabayasi masayasu & kasuga tosiaki

```
よく使うのは2次元配列を１次元配列にする

```js
const favorite = [['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'], ['yamasato', 'ryota', 'yamasaki', 'sizuyo']];

const result = favorite.reduce((ret, cur) => ([...ret, ...cur]), []);
console.log(result);
// 出力 ['wakabayasi','masayasu','kasuga','tosiaki','yamasato','ryota','yamasaki','sizuyo']
```

##sort
・アルファベット

```js
const favorites = [{name: "waka"}, {name: "masa"},{name: "kasu"},{name: "toshi"}];
console.log(favorites);
/* 出力 
[
  { name: 'waka' },
  { name: 'masa' },
  { name: 'kasu' },
  { name: 'toshi' }
]
*/

favorites.sort((a, b) => {
 if(a.name<b.name)return -1;
 if(a.name>b.name)return 1;
});
console.log(favorites);
/* 出力
[
  { name: 'kasu' },
  { name: 'masa' },
  { name: 'toshi' },
  { name: 'waka' }
]
*/
```

・数字(昇順）

```js
const favorites = [{id: 4}, {id: 3},{id: 2},{id: 1}];
console.log(favorites);
// 出力 [ { id: 4 }, { id: 3 }, { id: 2 }, { id: 1 } ] 

favorites.sort((a, b) => {
 if(a.id<b.id)return -1;
 if(a.id>b.id)return 1;
});
console.log(favorites);
// 出力 [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
```
##some

```js
const favorite = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki', 'masayasu'];

const result1 = favorite.some(x => x.length > 8);
console.log(result1);
// 出力 true

const result2 = favorite.some(x => x.length > 10);
console.log(result2);
// 出力 false
```
##スプレット構文

```js
const favorite1 = ['wakabayasi', 'masayasu', 'kasuga', 'tosiaki'];
const favorite2 = ['yamasato', 'ryota', 'yamasaki', 'sizuyo'];

console.log(...favorite1);
// 出力 wakabayasi masayasu kasuga tosiaki
console.log(...favorite1, ...favorite2);
// 出力 wakabayasi masayasu kasuga tosiaki yamasato ryota yamasaki sizuyo
```
##Object.assign

```js
const favorite1 = {id1: 'wakabayasi', id2: 'masayasu'};
const favorite2 = {id3: 'kasuga', id4: 'tosiaki'};

const result = Object.assign(favorite1, favorite2);
console.log(result);
// 出力 { id1: 'wakabayasi', id2: 'masayasu', id3: 'kasuga', id4: 'tosiaki' }
```

##Object.entries

```js
const favorite1 = {id1: 'wakabayasi', id2: 'masayasu'};

for(const [key, value] of Object.entries(favorite1)){
      console.log(`${key}:${value}`);    
}
// 出力1 id1: wakabayasi
// 出力2 id2: masayasu
```

##アロー関数

```js
var func1 = x => x + ' ' + 'masayasu';
var func2 = (x, y) => { return x + ' ' + y; };

console.log(func1('wakabayasi'));
// 出力 wakabayasi masayasu
console.log(func2('kasuga', 'tosiaki'));
// 出力 kasuga tosiaki
```

##filter + includes
やりたいこと
配列に格納したオブジェクトから、指定の値を含むものを抽出する

```js
const favorites = [{name: "waka"}, {name: "masa"},{name: "kasu"},{name: "toshi"}]
const targets = ["waka", "kasu"]

const result = favorites.filter(favorite => targets.includes(favorite.name));
console.log(result)
// 出力 [ { name: 'waka' }, { name: 'kasu' } ]
```