1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

機械学習エンジニアのためのjavascript 早見表

Last updated at Posted at 2020-07-19

機械学習エンジニアだとPython をよく使うことはあるが、ついついjavascript の使い方を忘れてしまうのではと思い、以下のような早見表を作った。ほぼ、自分のための備忘録ではあるが・・・

下記は node で実行。

誰向け?

  • javascript そんなに使わない。けど、時には必要かも・・・・
  • python よく使う人

operation

=== //type and value
== //value
% //mod
... // spreads
Array.from(tf_arr.keys()) // spreads same

arrow

(python でいう lambda)

single

> fake_func=() => 42
> fake_func()
42

multiple

> res=() => (x) => x*2
[Function: res]
> double=res()
[Function (anonymous)]
> double(3)
6

array

push

> var res=[];res.push(1)
1

pop

> res.pop()
1

splice

> var arr=[1,2,3,4,5,6];res = arr.splice(0,3); 
[ 1, 2, 3 ]
> arr
[ 4, 5, 6 ]

slice

> arr=[1,2,3,4,5,6];
[ 1, 2, 3, 4, 5, 6 ]
> arr.slice(0,3)
[ 1, 2, 3 ]
> arr
[ 1, 2, 3, 4, 5, 6 ]

length

arr.length

ForEach 
(これより先は、ほぼarrow 関数を入れる感じ)

> var res=[]
undefined
> arr=[1,2,3,4,5,6];
[ 1, 2, 3, 4, 5, 6 ]
> arr.forEach((z)=>{res.push(z)})
undefined
> res
[ 1, 2, 3, 4, 5, 6 ]

map

> arr.map((x)=>{return 2*x})
[ 2, 4, 6, 8, 10, 12 ]

filter

> arr.filter((x)=>{return x%2===0})
[ 2, 4, 6 ]

reducer

> arr.reduce((stock,x)=>{return stock + x})
15
> arr.reduce((stock,x)=>{return stock + x},100)
115

keys (index)

> var tf_arr=[true,false,true,false,true,true]
> tf_arr.keys()
Object [Array Iterator] {}
> [...tf_arr.keys()]
[ 0, 1, 2, 3, 4, 5 ]

get keys of true

> [...tf_arr.keys()].filter((x)=>{return tf_arr[x]})
[ 0, 2, 4, 5 ]

same value array

> Array(4).fill(true)
[ true, true, true, true ]

zip

> zip=(x,y)=>{return x.map((v,k)=>{return [v,y[k]]})}
> zip([1,2,3],[4,5,6])
[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]

for loops

iterrate

> for (const x in tf_arr){console.log(x)}
0
1
2
3
4
5

key only

> dict={"a":1,"b":2}
> for (key in dict){console.log(key)}
a
b

key value only

> for (const [key, value] of Object.entries(dict)) {console.log(key,value)}
a 1
b 2

enumerate

> for (const [k,v] of tf_arr.entries()){console.log(k,v)}
0 true
1 false
2 true
3 false
4 true
5 true

補足

arrow と func 一緒じゃない? => 違う

挙動は同じ。けど、this 等が参照できない。Vue, React 使う場合はこれを気をつけるべき!

詳細は下記参照

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?