Find an array index of the substitute value that should be chosen when a given index is deleted
npm install sub-index




Find an array index of the substitute value that should be chosen when a given index is deleted
``javascript
import subIndex from 'sub-index';
const array = new Array(3);
subIndex(array, 1); //=> 0
subIndex(array, 2); //=> 1
subIndex(array, 3); //=> 2
subIndex(array, 0); //=> 1
`
``
npm install sub-index
``
bower install sub-index
array: Array (non-empty array) Number
index: (index of the array) Number
Return: or null
Essentially, it returns the predecessor number of a given index.
``
index: 0 1 2
value: A B C
deleted: ^
substitute: ^
`javascript`
subIndex(['A', 'B', 'C'], 1); //=> 0
If index is 0, it returns the successor number 1 because the first element has no predecessor elements.
``
index: 0 1 2
value: A B C
deleted: ^
substitute: ^
`javascript`
subIndex(['A', 'B', 'C'], 0); //=> 1
If the array includes only a single value, it returns null because no value exists after deletion.
``
index: 0
value: A
deleted: ^
substitute: (none)
`javascript``
subIndex(['A'], 0); //=> null
Copyright (c) 2016 Shinnosuke Watanabe
Licensed under the MIT License.