Thursday, November 12, 2020

Arrays 1

// How to create an Array. How to make a new array and add something in the middle.

const bikes = ['Bianchi', 'Miele', 'Panasonic', 'Miyata'];

 const newBikes = [
...bikes.slice(0, 2),
'Benotto',
...bikes.slice(2)
]

// How to add something by spreading:

const names2 = [...names, 'Lux'];
const names3 = ['Poppy', ...names]

// slice is immutable
// returns shallow copy, the end is not inclusive


// splice is mutable
// take the item out of the original array
// start and how many to go for

// How to find the index of something by its id:

function deleteComment(id, comments) {
const commentIndex = comments.findIndex(comment => comment.id === id);
return [
...comments.slice(0, commentIndex),
...comments.slice(commentIndex + 1),
]}

// How to find the index of something by the exact thing inside the array:

const kaitIndex = names.findIndex(name => name === 'Kate');
console.log(kaitIndex);

No comments:

Post a Comment

Arrays 4 : Callback Methods find() filter() some() every() sort()

  /* Callback Methods */ // const util = { // findBurgRating: function(singleFeedback) { // return singleFeedback.comment....