_.sortedIndex(array, value, [iteratee], [context])
- array
- array
- value
- mixed
- [iteratee]
- int
- [context]
- iteratee関数内のthis
第1引数の配列にvalueを挿入できるIndexを返す。
iteratee関数が提供されている場合は、渡した値も含めて、各値のソート順位の計算に使用されます。
反復対象は、ソートするプロパティの文字列名(たとえば、length)でも構いません。
サンプル
Javascript
const debug1 = new Debug('#code1-dump');
$('span#code1-execute').on('click', (e) => {
const value = [10, 20, 30, 40, 50];
debug1.dump(_.sortedIndex(value, 35));
});
$('span#code1-console-clear').on('click', (e) => {
debug1.clear();
});
HTML
<span class="cq-btn m-success" id="code1-execute">実行</span>
<span class="cq-btn m-danger" id="code1-console-clear">削除</span>
<div id="code1-dump"></div>
サンプル
Javascript
const debug2 = new Debug('#code2-dump');
$('span#code2-execute').on('click', (e) => {
const search = [
{age: 1996, title: '赤・緑・青'},
{age: 1998, title: 'ピカチュウ'},
{age: 1999, title: '金・銀'},
{age: 2000, title: 'クリスタルバージョン'},
{age: 2002, title: 'ルビー・サファイア'},
{age: 2004, title: 'ファイアレッド・リーフグリーン/エメラルド'},
{age: 2006, title: 'ダイヤモンド・パール'},
{age: 2009, title: 'ハートゴールド・ソウルシルバー'},
{age: 2010, title: 'ブラック・ホワイト'},
{age: 2012, title: 'ブラック2・ホワイト2'},
{age: 2013, title: 'X・Y'},
{age: 2014, title: 'オメガルビー・アルファサファイア'},
{age: 2016, title: 'サン・ムーン'},
{age: 2017, title: 'ウルトラサン・ウルトラムーン'},
{age: 2018, title: 'Let\'s Go! ピカチュウ・Let\'s Go! イーブイ'}
];
debug2.dump(_.sortedIndex(search, {age: 2008, title: 'プラチナ'}, 'age'));
const idx = _.sortedIndex(search, {age: 2008, title: 'プラチナ'}, function (v) {
debug2.dump(this, v);
return v.age;
}, {a: 'xxx'});
debug2.dump(idx);
});
$('span#code2-console-clear').on('click', (e) => {
debug2.clear();
});
HTML
<span class="cq-btn m-success" id="code2-execute">実行</span>
<span class="cq-btn m-danger" id="code2-console-clear">削除</span>
<div id="code2-dump"></div>