Using .splice();
- When you use splice
you are modify/editing the array
- deleteCount
is how many indexes you want to remove
- itemN
is if you want to replace the index
with different content
*Syntax array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Using .slice();
- The main difference is that slice
you don't modify the original array. Just cut it choosing the start & end index inside the brackets, returning you a copy of that new array.
- In the brackets you can set only the starting
point, and the end
will be assigned auto as the end of the array
*Syntax arr.slice([begin[, end]])
ex.
var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
console.log(array);
// shows [1, 2], original array altered.
//////////
var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
Ref:
- http://www.tothenew.com/blog/javascript-splice-vs-slice/
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
Using .substr();
- Takes parametrs at (from, length)
*Syntax str.substr(start[, length])
Using .substring();
- Takes parametrs (from, to)
*Syntax str.substring(indexStart[, indexEnd])
ex.
const name = "Daniels";
console.log(name.substring(2, 5)); // Show in the console -> "nie"
console.log(name.substr(2,5)); // Show in the console -> "niels"
Ref:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Using .indexOf();
- The
indexOf()
method returns theindex
within the callingString
object of the first occurrence of the specified value, starting the search atfromIndex
. Returns -1 if the value is not found. fromIndex
in the brackets is optional- It is case sensitive
*Syntax
str.indexOf(searchValue[, fromIndex])
ex.
'Blue Whale'.indexOf('Blue'); // returns 0
'Blue Whale'.indexOf('Blute'); // returns -1
'Blue Whale'.indexOf('Whale', 0); // returns 5
'Blue Whale'.indexOf('Whale', 5); // returns 5
'Blue Whale'.indexOf('Whale', 7); // returns -1
'Blue Whale'.indexOf(''); // returns 0
'Blue Whale'.indexOf('', 9); // returns 9
'Blue Whale'.indexOf('', 10); // returns 10
'Blue Whale'.indexOf('', 11); // returns 10
Using .lastIndexOf();
- The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from fromIndex. Returns -1 if the value is not found.
- Same thing as
.indexOf();
but is searching backwards fromIndex
is optional- Case sensitive as well
-
- Displays the last index that the string contains, check the example
*Syntax
str.lastIndexOf(searchValue[, fromIndex])
- Displays the last index that the string contains, check the example
*Syntax
ex.
'canal'.lastIndexOf('a'); // returns 3
'canal'.lastIndexOf('a', 2); // returns 1
'canal'.lastIndexOf('a', 0); // returns -1
'canal'.lastIndexOf('x'); // returns -1
'canal'.lastIndexOf('c', -5); // returns 0
'canal'.lastIndexOf('c', 0); // returns 0
'canal'.lastIndexOf(''); // returns 5
'canal'.lastIndexOf('', 2); // returns 2
Ref: