Rubyとの比較とか
- JavaScript | MDN
- JavaScript初級者から中級者になろう — uhyohyo.net
- 文字列操作の比較表: Ruby, Python, JavaScript, Perl, C++
- 配列操作の比較表: Ruby, Python, JavaScript, Perl, C++
String
Ruby | JavaScript |
---|---|
[] slice | String.prototype.charAt() |
size length | String.length |
to_i | Number() parseInt() |
to_f | Number() parseFloat() |
split | String.prototype.split() |
chomp | String.prototype.trim() |
chop | ??? |
[] #slice
rb
'abc'[1] # => 'b'
'abc'.slice(1) # => 'b'
js
'abc'.charAt(1) // 'b'
#size #length
rb
'abc'.size # => 3
'abc'.length # => 3
js
'abc'.length // 3
#to_i
rb
'1'.to_i # => 1
'1.1'.to_i # => 1
js
Number('1') // 1
parseInt('1') // 1
parseInt('1.1') // 1
#to_f
rb
'1'.to_f # => 1.0
'1.1'.to_f # => 1.1
js
Number('1') // 1
parseFloat('1') // 1
parseFloat('1.1') // 1.1
#split
rb
'abc'.split('') # => ["a", "b", "c"]
'abc'.chars # => ["a", "b", "c"]
js
'abc'.split('') // ["a", "b", "c"]
#chomp
rb
"\na\nb\nc\n".chomp # => "\na\nb\nc"
js
"\na\nb\nc\n".trim() // "a\nb\nc"
Numeric
Ruby | JavaScript |
---|---|
to_s | Number.prototype.toString() |
ceil | Math.ceil() |
floor | Math.floor() |
#to_s
rb
1.to_s # => "1"
1.1.to_s # => "1.1"
js
Number('1').toString() // "1"
Number('1.1').toString() // "1.1"
#ceil
rb
1.ceil # => 1
1.1.ceil # => 2
js
Math.ceil(1) // 1
Math.ceil(1.1) // 2
#floor
rb
1.floor # => 1
1.9.floor # => 1
js
Math.floor(1) // 1
Math.floor(1.9) // 1
Array
Ruby | JavaScript |
---|---|
count size length | Array.length |
join | Array.prototype.join() |
sort | Array.prototype.sort() |
reverse | Array.prototype.reverse() |
each | Array.prototype.forEach() |
map collect | Array.prototype.map() |
select | Array.prototype.filter() |
inject #reduce | Array.prototype.reduce() |
any? | Array.prototype.some() |
all? | Array.prototype.every() |
include? | Array.prototype.indexOf() |
include? | Array.prototype.includes() |
delete | ??? |
empty? | ??? |
first | ??? |
last | ??? |
push | ??? |
pop | ??? |
unshift | ??? |
shift | ??? |
compact | ??? |
uniq | ??? |
#count #size #length
rb
[1, 2, 3].count # => 3
[1, 2, 3].size # => 3
[1, 2, 3].length # => 3
js
[1, 2, 3].length // 3
#join
rb
[1, 2, 3].join('/') # => "1/2/3"
js
[1, 2, 3].join('/') // '1/2/3'
#sort #reverse
rb
[1, 2, 3].reverse # => [3, 2, 1]
[1, 2, 3].reverse.sort # => [1, 2, 3]
js
[1, 2, 3].reverse(); // [3, 2, 1]
[1, 2, 3].reverse().sort(); // [1, 2, 3]
Array.prototype.sort()
Array.prototype.reverse()
#each
rb
[1, 2, 3].each { |v| puts v }
js
[1,2,3].forEach(function(v) { console.log(v) });
#map #collect
rb
[1, 2, 3].map { |v| v.to_s } # => [ '1', '2', '3' ]
js
[1, 2, 3].map(function(v) { return v.toString() }); // [ '1', '2', '3' ]
#select
rb
[1, 2, 3].select {|v| v.odd? } # => [1, 3]
js
[1, 2, 3].filter(function(v) { return v % 2 != 0 }); // [ 1, 3 ]
#inject #reduce
rb
[1, 2, 3].inject(&:+) # => 6
js
[1, 2, 3].reduce(function(r, v) { return r + v }); // 6
#any?
rb
[1, 2, 3].any? { |v| v.even? } # => true
js
[1, 2, 3].some(function(v) { return v % 2 == 0 }); // true
#all?
rb
[2, 4, 6].all? { |v| v.even? } # => true
js
[2, 4, 6].every(function(v) { return v % 2 == 0 }); // true
#include?
rb
[1, 2, 3].include?(2) # => true
js
[1, 2, 3].indexOf(2) >= 0 // true
js
// This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
[1, 2, 3].includes(2) // true
Hash
Ruby | JavaScript |
---|---|
keys | Object.keys() |
values | ??? |
each | ??? |
map | ??? |
size | ??? |
empty? | ??? |
key? #has_key? | ??? |
value? #has_value? | ??? |
#keys
rb
{a: 1, b: 2}.keys # => [:a, :b]
js
Object.keys({a: 1, b: 2}) // [ 'a', 'b' ]
Range
(1..10).to_a
[...Array(10).keys()]
// => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Array.from([...Array(10).keys()], (v, i) => i + 1)
// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
JavaScript ガイド
Statements
オブジェクト操作文
for ... in
var obj = {a: 1, b: 2};
for (var key in obj) {
console.log(key + '=[' + obj[key] + ']');
};
// a=[1]
// b=[2]
var obj = [1, 2];
for (var idx in obj) {
console.log(idx + '=[' + obj[idx] + ']');
};
// 0=[1]
// 1=[2]