ruby のあれは、 node.js でどう書くんだ

js むずい


|ruby|node.js|
|:--|:--|
|File.read('file.txt')|const fs = require('fs')<br>fs.readFileSync('file.txt', 'UTF-8') |
|File.write('file.txt', content)|fs.writeFileSync('file.txt', content) |
|a = [ 0, 1, 2, 3, 4, 5 ]<br>a.delete_at(3) # => 3<br>a # =>[ 0, 1, 2, 4, 5 ]<br>|const a = [ 0, 1, 2, 3, 4, 5 ]<br>a.splice(3, 1) // => [ 3 ]<br>a // =>[ 0, 1, 2, 4, 5 ]<br>|
|a = [ 0, 7, 2, 5, 9, 1 ]<br>a.max # => 9|const a = [ 0, 7, 2, 5, 9, 1 ]<br>Math.max(a) // => 9|
|a = [ 1, 2, 3 ]<br>a.each { &#124;a&#124; puts a }|const a = [ 1, 2, 3 ] <br>for(let m of a) { console.log(a) }|
|h = {a: 1, b: 2, c: 3}<br>h.each { &#124;k, v&#124; puts v }|const o =  {a: 1, b: 2, c: 3} <br>for(let k of Object.keys(o)) {<br> console.log(o[k])<br> }|
|s = '123'<br>s.to_i # => 123|const s = '123'<br>parseInt(s, '10') // => 123|
|o = {a: 1, b: 2, c: 3, d: 4, e: 5}<br>pluck = [:a, :c, :e].map { &#124;k&#124; [k, o[k]] }.to_h<br># => {a: 1, c: 3, e: 5}|const o = {a: 1, b: 2, c: 3, d: 4, e: 5}<br>const pluck = (({a, c, e}) => ({a, c, e}))(o)<br>// => {a: 1, c: 3, e: 5}|
|s = 'asdfasdf'<br>s.gsub('df', 'DF') # => 'asDFasDF'|const s = 'asdfasdf'<br>s.replace(/df/g, 'DF') // => 'asDFasDF'|
|require 'YAML'<br>y = YAML.load_file('a.yml')|const fs = require('fs')<br>const YAML = require('yaml')<br>y = YAML.parse(fs.readFileSync('a.yml', 'utf8') |
|h = [{d: 3}, {d: 10}, {d: 12}]<br>h.any? { &#124;r&#124; 9 < r[:d] } # => true|const o = [{d: 3}, {d: 10}, {d: 12}]<br>o.some((r) => (9 < r.d)) // => true|
|a = [{d: 30}, {d: 10}, {d: 3}]<br>index = a.index { &#124;r&#124; r[:d] == 3 } # => 2|const a = [{d: 30}, {d: 10}, {d: 3}]<br>const index = a.findIndex(r => r.d === 3) // => 2|
|val = 'AAA'<br>"embed#{val}value" # => 'embedAAAvalue'|const val = 'AAA'<br>&#x60;embed${val}value&#x60; // => 'embedAAAvalue'
|a = [1, 2, 3]<br>b = ['a', 'b', 'c']<br>zip = a.zip(b)<br># => [[1, 'a'], [2, 'b'], [3, 'c']]|const a = [1, 2, 3]<br>const b = ['a', 'b', 'c']<br>const zip = a.map((e, i) => [e, b[i]])<br>// => [[1, 'a'], [2, 'b'], [3, 'c']]|
|a = ['a', 'b', 'c']<br>mapped = a.map { &#124;m&#124; m * 2 } # => ['aa', 'bb', 'cc']|const a = ['a', 'b', 'c']<br>const mapped = a.map(m => (m + m)) // => ['aa', 'bb', 'cc']|
|a = [1, 2, 3]<br>sum = a.reduce(0) do &#124;memo, cur&#124;<br>&nbsp;&nbsp;memo += cur<br>&nbsp;&nbsp;memo<br>end<br># => 6|const a = [1, 2, 3]<br>const sum = a.reduce((memo, cur) => {<br>&nbsp;&nbsp;memo += cur<br>&nbsp;&nbsp;return memo<br>}, 0)<br>// => 6|
|a = ['a', 'bb', 'c', 'ddd', 'e']<br>selected = a.select { &#124;m&#124; m.length == 1 } # => ['a', 'c', 'e']|const a = ['a', 'bb', 'c', 'ddd', 'e']<br>const selected = a.filter(m => (m.length === 1)) // => ['a', 'c', 'e']|
|a = ['a', 'b', 'c']<br>a.map.with_index { &#124;m, idx&#124; "#{m}-#{idx}" } # => ["a-0", "b-1", "c-3"]|const a = ['a', 'b', 'c']<br>a.map((m, idx) => (m + '-' + idx;)) // => ["a-0", "b-1", "c-3"]|
|a = [1, 2, 3]<br>a.clear<br>a # => []|const a = [1, 2, 3]<br>a.length = 0<br>a // => []|
|a = []<br>a.empty? # => true|const a = []<br>a.length === 0 // => true|
|a = [1, 2, 3]<br>a.shift # => 1<br>a.unshift 5 # => [5, 2, 3]<br>a.pop # => 3<br>a << 6 # => [5, 2, 6]<br>|const a = [1, 2, 3]<br>a.shift() // => 1<br>a.unshift(5)<br>a.pop() // => 3<br>a.push(6)<br>a // => [5, 2, 6]|
|a = [1, 2, 3]<br>a.find { &#124;m&#124; 1 < m } # => 2|const a = [1, 2, 3]<br>a.find(m => (1 < m) } // => 2|
|a = [1, 2, 3]<br>a.all? { &#124;m&#124; 0 < m } # => true|const a = [1, 2, 3]<br>a.every(m => (0 < m)) // => true|




つど調べたら追記!!

---

* Array.splice(https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)

```
var arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
```