ソート、重複除く、などJavascript memo
- 昇順
array.sort( function(val1,val2){ if(val1.propety1< val2.propety1) return -1; if(val2.propety1< val1.propety1) return 1; return 0 });
それ以外はこちら
JavaScript つい忘れてしまう配列のソート方法 - Qiita
- オブジェクトの中身拝見
console.log(JSON.stringify(arrayObj));
- 重複除く
var hoge = foo.filter(function(x,i,self){return self.indexOf(x) ===i});
- 同じキーがあるかどうか
Object.keys(dataObj).filter(function(k) { return dataObj[k] == searchword})[0] // ない場合 === undefined
- 金額のカンマ編集
1000.toString().replace(/(\d)(?=(\d{3})+$)/g , '$1,')
- 今日の日付を書式yyyymmdd(月と日を必要に応じて0埋め)で取得する。
var now = new Date(); var yyyymmdd = now.getFullYear()+ ( "0"+( now.getMonth()+1 ) ).slice(-2)+ ( "0"+now.getDate() ).slice(-2); var tomorrow = now.getFullYear()+ ( "0"+( now.getMonth()+1 ) ).slice(-2)+ ( "0"+( now.getDate()+1) ).slice(-2);
- 配列→文字列
区切り文字列で結合
ary = ['hoge','foo','bar','piyo']; str = ary.join(','); console.log(str); //=>hoge,foo,bar,piyo
- 文字列→配列
区切り文字列で配列にする。
str = 'hoge,foo,bar,piyo'; ary = str.split(','); console.log(ary); //=>["hoge", "foo", "bar", "piyo"]
- 小数点の編集
・ 切捨て
Math.floor(val)
・ 切り上げ
Math.ceil(val)
・ 四捨五入
Math.round(val)
MEANスタックの環境をつくる(途中)
OS: CentOS 6.7
Node.jsとnpmをinstall
sudo yum install epel-release
sudo yum install nodejs
node -v
バージョンでたらインストール完了
yum install -y npm --enablerepo=epel
npm も入れとく
Expressいれる
npm install express
MVSサイト構築
express コマンドを使ってサイトの雛形生成からサイトの動作確認まで行う
mkdir dev cd dev express myapp -e ejs cd myapp npm install npm start
サーバーが立ち上がったらローカルホスト:3000 で確認。
bootstrapをいれる
見た目よくするためにbowerを使ってインストール。
インストールしたら初期化しておく。
npm install -g bower
bower init
初期化の質問は下記参照:
Bowerまとめ(概要・導入・コマンド一覧) - Qiita
作業DIR上でbootstrapインストール ※依存関係にあるjqueryも同時に入る
bower install bootstrap --save
あとはMongoとAngular入れてひと段落。
sublime3 のPackage Controlのインストール
- Proxyなしの場合
import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
- Proxy環境下の場合
http://user:pass@address:port のユーザー名やパスワードは各自設定。
import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler('http': 'http://user:pass@address:port')) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
とある文字以降を削除
以降を削除、という書き方は以下。
.+
. は改行を除く任意の一文字のこと。
- は直前の文字列やメタ文字の1回以上の繰り返しのこと。
例)空白以降を削除
.+
Gitでうっかり違うメアドでコミットしちゃった件
なんか知らない人がコミットしているというのがばれないように後で修正する。
rebaseの魔法を唱える
$ git rebase -i HEAD~2
エディタが開いて、行頭に pick がついたリストが出てくるので、
修正対象を「edit」に修正して:wq。
すると修正チャンス到来。
$ git rebase -i HEAD~2 Stopped at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx... hoge You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue
メアドとユーザーを変更してコミット。
shinomiya@foo ~foo (branch|REBASE-i 2/2) $ git commit --amend --author="user.name <user@example.com>" [detached HEAD 0000000] hoge Date: Thu Jun 16 19:46:27 2016 +0900 1 file changed, 18 insertions(+), 18 deletions(-)
あとはrebase --continue で残りのコミットを走らせる。
shinomiya@foo ~foo (branch|REBASE-i 2/2) $ git rebase --continue Successfully rebased and updated refs/heads/branch.
Rails memo
Railsの話聞いてきた。
Railsの学びとキャリア02 ~未経験から無料のワンポイント研修とキャリアのノウハウ~ - Railsの学びと就業 | Doorkeeper
お手軽に環境が作れる話とRails(Silver)問題集が1000円で買えるという話につられてつい。
ネット環境(Wifi)がなかったので自前のテザリングで参加。
※会場での借りる手続きがうまくされていなかった模様。
お手軽に環境作る話
- Cloud9
割りとお手軽に環境を作れるサービス。
Web上で動いてる。
サーバーはUbuntu。
c9.io
- Cloud9内のRailsはバージョンが古いので
新しい方いれとく(古いままだと動かないことがあるらしい)
gem install rails -v 4.2.2
- DBはPostgres。
installしとく(Postgres)
sudo service postgresql start
ログ:
shinomiya_1:~/workspace $ sudo service postgresql start * Starting PostgreSQL 9.3 database server ...done. shinomiya_1:~/workspace $ sudo service postgresql stop * Stopping PostgreSQL 9.3 database server ...done. shinomiya_1:~/workspace $
- Cloud9コマンド
Cloud9の中のパラメータを引数として渡さんとあかんらしい。
→サーバー起動時のコマンド例
rails s -b $IP -p $PORT
- Railsの試験勉強するにはCloud9はらくだね。
Railsの擬似問題
@ITに結構のっているので練習するにはOK。
- Cloud9 上でRailsを作成する
今回は「achieve」という名前で作成。DBはpostgresql
rails _4.2.2_ new achieve -d postgresql
ログ:
shinomiya_1:~/workspace $ rails _4.2.2_ new achieve -d postgresql create (省略) run bundle install Fetching gem metadata from https://rubygems.org/ Fetching version metadata from https://rubygems.org/ Fetching dependency metadata from https://rubygems.org/ Resolving dependencies.......... Installing rake 11.1.2 Using i18n 0.7.0 Using json 1.8.3 Installing minitest 5.9.0 Using thread_safe 0.3.5 Using builder 3.2.2 Using erubis 2.7.0 Using mini_portile2 2.0.0 Using rack 1.6.4 Using mime-types-data 3.2016.0221 Using arel 6.0.3 Installing debug_inspector 0.0.2 with native extensions Using bundler 1.12.4 Installing byebug 9.0.4 with native extensions Installing coffee-script-source 1.10.0 Installing execjs 2.7.0 Using thor 0.19.1 Using concurrent-ruby 1.0.2 Installing multi_json 1.12.1 Installing pg 0.18.4 with native extensions Installing sass 3.4.22 Installing tilt 2.0.4 Installing spring 1.7.1 Installing rdoc 4.2.2 Using tzinfo 1.2.2 Using nokogiri 1.6.7.2 Using rack-test 0.6.3 Using mime-types 3.0 Installing binding_of_caller 0.7.2 with native extensions Installing coffee-script 2.4.1 Installing uglifier 3.0.0 Using sprockets 3.6.0 Installing sdoc 0.4.1 Using activesupport 4.2.2 Using loofah 2.0.3 Using mail 2.6.4 Using rails-deprecated_sanitizer 1.0.3 Using globalid 0.3.6 Using activemodel 4.2.2 Installing jbuilder 2.4.1 Using rails-html-sanitizer 1.0.3 Using rails-dom-testing 1.0.7 Using activejob 4.2.2 Using activerecord 4.2.2 Using actionview 4.2.2 Using actionpack 4.2.2 Using actionmailer 4.2.2 Using railties 4.2.2 Using sprockets-rails 3.0.4 Installing coffee-rails 4.1.1 Installing jquery-rails 4.1.1 Using rails 4.2.2 Installing sass-rails 5.0.4 Installing web-console 2.3.0 Installing turbolinks 2.5.3 Bundle complete! 12 Gemfile dependencies, 55 gems now installed. Use `bundle show [gemname]` to see where a bundled gem is installed. Post-install message from rdoc: Depending on your version of ruby, you may need to install ruby rdoc/ri data: <= 1.8.6 : unsupported = 1.8.7 : gem install rdoc-data; rdoc-data --install = 1.9.1 : gem install rdoc-data; rdoc-data --install >= 1.9.2 : nothing to do! Yay! run bundle exec spring binstub --all * bin/rake: spring inserted * bin/rails: spring inserted shinomiya_1:~/workspace $
Cloud9内部にGitが入っている
nameと emailは適当でもOK。
HerokuのToolbeltが入っているので
Cloud9で作成したアプリをHerokuに公開することもできる。
(Herokuのアカウントを作っておくこと)