Split Join Slice Splice
String.split(separator)¶
split(separator)is a string function which divides a string into an array based on some delimiter.
let sentence = "sally sells sea shells by the sea shore";
let wordArray = sentence.split(" ");
wordArray; // ["sally", "sells", "sea", "shells", "by", "the", "sea", "shore"]
join(separator)¶
- Returns a concatenated string of all of the values in an Array (defaults to commas);
let arr = [1,true,'hello','mango'];
let joined = arr.join();
joined; // '1,true,hello,mango'
let spaced = arr.join(' ');
spaced; // '1 true hello mango'
let and = arr.join(' and ');
and; // '1 and true and hello and mango'
slice(startIndex, endIndex)¶
- Returns a slice (subarray) of the array from the index specified at the first argument up to (but not including) the number provided at the index specified by the second argument. If no second argument is provided, it will create a subarray from the start index to the end of the array. NOTE
sliceis non-destructive. It returns a subarray, but does not alter the original.
let arr = ['zero','one','two','three'];
let sub = arr.slice(2);
sub; // [ 'two', 'three' ]
let sub2 = arr.slice(0,2);
sub2; // [ 'zero', 'one' ] => Note the non-inclusivity
arr; // [ 'zero', 'one', 'two', 'three' ] => Non-destructive
Skill++¶
Remember that assigning an array to a variable assigns a reference to the original array:
With no arguments,let arrRef = arr; console.log(arrRef === arr); // trueslicereturns a complete copy of the original array:This is an easy way to create a copy of an array. Another way is to use the spread opertator:let arrCopy = arr.slice(); console.log(arrCopy === arr); // falselet arrCopy2 = [...arr]; console.log(arrCopy2 === arr); // false
splice(delStartIndex, delEndIndex, itemToInsert, ...)¶
- Destructively deletes, inserts, or both inserts and deletes from an Array, modifying the original array in the process.
- The first argument specifies the index to begin the deletion/insertion.
- The second argument indicates how many indices to remove after the starting point. (NOTE providing a
0will lead to no deletions). - All subsequent arguments provided will be inserted into the array at the index specified by the first argument.
splicereturns an array of deleted items.
let ogArray = ['apple', 'banana', 'coconut'];
ogArray.splice(1); // [ 'banana', 'coconut' ] => returns deleted elements
ogArray; // [ 'apple' ] => destructive update of original
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
let removeTheMiddleArray = ['Reese', 'Malcom', 'Dewey'];
removeTheMiddleArray.splice(1,1); // [ 'Malcom' ]
removeTheMiddleArray; // [ 'Reese', 'Dewey' ]
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// There should be a 4 where it says 'kiwi'
let incorrectArray = [1,2,3,'kiwi',5,6,7];
incorrectArray.splice(3,1,4); // [ 'kiwi' ] => remove 'kiwi'
incorrectArray; // [ 1, 2, 3, 4, 5, 6, 7 ] => 4 has been inserted
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Add several items
let missingData = ['death', 'taxes'];
missingData.splice(1,0,'bananas','jobs','school','friends'); // [] => nothing deleted
missingData; // [ 'death', 'bananas', 'jobs', 'school', 'friends', 'taxes' ]
Hands On:¶
Create a variable
datawhich stores a String of comma-separated words as values.Use
split()ondataand assign the resulting array to a variabledataArray.Use
joinon thedataArrayto concatenate all of the values with//and store the string back into thedatavariable.Create a new variable named
initialData, and usespliceondataArrayto store only the first two data points intoinitialData.Use
spliceoninitialDatato insert"First Data Point"at the 0th index.Use
spliceoninitialDataagain to insert"Third Data Point"at the 2nd index.Use
spliceoninitialDataagain to delete everything after the first data point (1st index).