Help us understand the problem. What is going on with this article?

# [JavaScript] 脱C言語風 配列操作コーディング

More than 3 years have passed since last update.

JavaScript学習の一助になればと思い3秒で作成しました．

`let` は大体 `var` みたいなもんです．
`v => ...` は大体 `function(v) { return ...; }` みたいなもんです．

## 配列を調べる/配列を加工するケース

Arrayのプロトタイプメソッドはチェインして使うと面白いです．

```arr.filter(v => v > 100).map(v => v + 5).reduce((sum, v)=> sum + v);
```

### 配列の要素に含むか？ includes

for
```let result = false;

for (let i = 0; i < arr.length; i++) {
if (arr[i] == target) {
result = true;
break;
}
}
```
includes
```let result = arr.includes(target);
```

### 配列の要素に条件を満たすものがあるか？ some

for
```let result = false;

for (let i = 0; i < arr.length; i++) {
if (arr[i] > target) {
result = true;
break;
}
}
```
some
```let result = arr.some(v => v > target);
```

### 配列の要素すべてが条件を満たすか？ every

for
```let result = true;

for (let i = 0; i < arr.length; i++) {
if (!(arr[i] > target)) {
result = false;
break;
}
}
```
every
```let result = arr.every(v => v > target);
```

### 配列の要素から条件を満たすものだけを残す filter

for
```let result = [];
let resultCount = 0;

for (let i = 0; i < arr.length; i++) {
if (arr[i] > target) {
result[resultCount++] = arr[i];
}
}
```
filter
```let result = arr.filter(v => v > target);
```

### 配列の要素から条件を満たすもの1つを取り出す find

for
```let result = null;

for (let i = 0; i < arr.length; i++) {
if (arr[i] > target) {
result = arr[i];
break;
}
}
```
find
```let result = arr.find(v => v > target);
```

### 配列の要素をすべて繋げて文字列にする join

for
```let result = "";

for (let i = 0; i < arr.length; i++) {
result += arr[i];
if (i != arr.length - 1) {
result += ",";
}
}
```
join
```let result = arr.join(",");
```

### 配列の要素それぞれを変換する map

for
```for (let i = 0; i < arr.length; i++) {
arr[i] = convert(arr[i]);
}
```
map
```let result = arr.map(v => convert(v));
```

### 畳み込み reduce reduceRight

for
```let result = 0;

for (let i = 0; i < arr.length; i++) {
result += arr[i];
}
```
reduce
```let result = arr.reduce((sum, v)=> sum + v, 0);
```

### 配列の部分配列をつくる slice

for
```let result = [];
let resultLength = 0;

for (let i = 2; i < 5; i++) {
result[resultLength++] = arr[i];
}
```
slice
```let result = arr.slice(2, 5);
```

### 配列の要素を並び替える sort

for
```割愛
```
sort
```let result = arr.sort((a, b)=> a < b);
```

べんりです

## forの方がよいかもしれないケース

### 配列の要素1つ1つについて何かする

for...of
```for (let v of arr) {
doSomething(v);
}
```
forEach
```arr.forEach(v => doSomething(v));
```

### ただ単に何かを複数回実行したいだけ

for
```for (let i = 0; i < count; i++) {
doSomething();
}
```
forEach
```new Array(count).fill(0).forEach(()=> doSomething());
```

## 微妙な感じのケース

### 動的に長さを決定する配列を生成して，各要素には関数の戻り値を設定する．

for
```let arr = [];
for (var i = 0; i < len; i++) {
arr[i] = generateValue(i);
}
```
map
```let arr = new Array(len).fill(0).map((zero, i)=> generateValue(i));
```

https://note.ztrehagem.dev/
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away