Matrices
// splice() returns NEW array !!!
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2); //['today', 'was', 'great']
//*************************************************************************'*/
let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2); // newArray has the value ['really', 'happy']
//*************************************************************************'*/
// Add items with splice()
const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;
numbers.splice(startIndex, amountToDelete, 13, 14);
console.log(numbers);
// The second entry of 12 is removed, and we add 13 and 14 at the same index.
// The numbers array would now be [ 10, 11, 12, 13, 14, 15 ]
//*************************************************************************'*/
// Copying array items with splice()
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
//todaysWeather would have the value ['snow', 'sleet'], while weatherConditions
//would still have ['rain', 'snow', 'sleet', 'hail', 'clear'].
//In effect, we have created a new array by extracting elements from an existing array.
//*************************************************************************'*/
// slice() return NEW array (NOTICE slice()!!! )
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
//todaysWeather would have the value ['snow', 'sleet'], while weatherConditions
//would still have ['rain', 'snow', 'sleet', 'hail', 'clear'].
//*************************************************************************'*/
// Combine arrays with spread
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
// thatArray would have the value ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
//*************************************************************************'*/
// indexOf()
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
fruits.indexOf('dates');
fruits.indexOf('oranges');
fruits.indexOf('pears');
// indexOf('dates') returns -1, indexOf('oranges') returns 2, and
// indexOf('pears') returns 1 (the first index at which each element exists).
//*************************************************************************'*/
function filteredArray(arr, elem) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
// if elem is NOT in the array... push arr to the newArr
if (arr[i].indexOf(elem) === -1) newArr.push(arr[i]);
}
return newArr;
}
console.log(
filteredArray(
[
[3, 2, 3],
[1, 6, 3],
[3, 13, 26],
[19, 3, 9],
],
3 // <------------------------
)
);