Thursday, November 12, 2020

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

 /*
Callback Methods
*/


// const util = {
// findBurgRating: function(singleFeedback) {
// return singleFeedback.comment.includes('burg');
// }
// }

// function findBurgRating(singleFeedback, index, array) {
// return singleFeedback.comment.includes('burg')
// }

//rating => rating.comment.includes('burg')

// find the first rating that talks about a burger with find()
// const burgRating = feedback.find(findBurgRating);
// console.log(burgRating);

function findByWord(word) {
return function(singleFeedback) {
return singleFeedback.comment.includes(word);
}
}

const wordFinder = findByWord('Smoothie');
const findWord = feedback.find(wordFinder);
console.log(findWord);

// find all ratings that are above 2 with filter()

function filterByMinRating(minRating) {
return function (singleFeedback) {
return singleFeedback.rating > minRating;
}
}

const goodReviews = feedback.filter(filterByMinRating(4));
console.table(goodReviews)

// find all ratings that talk about a burger with filter()
const burgRatings = feedback.filter(singleFeedback => singleFeedback.comment.includes('burg'));
console.table(burgRatings)

// Remove the one star rating however you like!
// Para mostrar solamente los que son superiores a 1. Esto puede ser para un dropdown donde elijas los ratings de que numero para arriba o abajo quieres ver.
const legitRatings = feedback.filter(single => single.rating !== 1);
console.table(legitRatings);

// check if there is at least 5 of one type of meat with some()
// Cuando quieres saber si hay por lo menos un numero de algo. GIves a boolean.
const isThereEnoughOfAtLeastOneMeat = Object.values(meats).some(meatValue => meatValue >= 5);
console.log(isThereEnoughOfAtLeastOneMeat);


// make sure we have at least 3 of every meat with every()
// Cuando quieres saber si hay por lo menos un numero de TODO. GIves a boolean.
const isThereEnoughOfEvery = Object.values(meats).every(meatValue => meatValue >= 3);
console.log(isThereEnoughOfAtLeastOneMeat);


// sort the toppings alphabetically with sort()
const numbers = [1,2,100,3,200,400,155]
// const numbersSorted = numbers.sort(function(firstItem, secondItem) {
// if(firstItem > secondItem) {
// return 1;
// } else if(secondItem > firstItem) {
// return -1;
// } else {
// return 0;
// }
// });

const numbersSorted = numbers.sort(function(firstItem, secondItem) {
return firstItem - secondItem; //in forward order
// return secondItem - firstItem; //in inverse order
});
console.log(numbersSorted);

// sort the order totals from most expensive to least with .sort()
function numberSort(a, b) {
return b - a;
}

console.log(orderTotals.sort(numberSort));
// Sort the prices with sort()

const productsSortedByPrice = Object.entries(prices).sort(function(a, b) {
const aPrice = a[1];
const bPrice = b[1]
return bPrice - aPrice;
});
console.table(productsSortedByPrice);

Arrays 3 : Instance Methods pop() push() shift() unshift() slice() splice() indexOf() lastIndexOf() includes()

 /*
Instance Methods
*/

pop() push() shift() unshift() slice() splice() indexOf() lastIndexOf() includes()


// Display all bun types with " or " - use join()

// We have a string "hot dogs,hamburgers,sausages,corn" - use split() to turn it into a string
const foodString = "hot dogs,hamburgers,sausages,corn";
// console.log(foodString.split(','));
// console.log([...foodString]);

// console.log(foodString.split(''));
// console.log([...foodString]);
// These two work the same, it's because you're splitting with nothing.

// take the last item off toppings with pop()
const lastItem = toppings.pop();
// console.log(lastItem);

// add it back with push()
const t2 = toppings.push(lastItem);
// console.log(t2);

// take the first item off toppings with shift()
const firstItem = toppings.shift();
// console.log(firstItem);

// add it back in with unshift()
const t3 = toppings.unshift(firstItem);
// console.log(t3);

// Do the last four,but immutable (with spreads and new variables)
let newToppings = toppings.slice(0, toppings.length - 1);
newToppings = [...newToppings, toppings[toppings.length - 1]];
// console.log(newToppings);

newToppings = toppings.slice(1, toppings.length - 1);
newToppings = ['Mushrooms', ...newToppings];
// console.log(newToppings);

// Make a copy of the toppings array with slice()
const toppingsCopy = toppings.slice(0) // end is optional
// console.log(toppingsCopy);

// Make a copy of the toppings array with a spread
const toppingsCopy2 = [...toppings] // end is optional
// console.log(toppingsCopy2);

// take out items 3 to 5 of your new toppings array with splice()
// console.log(newToppings);
newToppings = newToppings.splice(2, 3);
// console.log(newToppings);

// find the index of Avocado with indexOf() / lastIndexOf()
const avoIndex = toppings.indexOf('Avocado');
// console.log(avoIndex);

const avoIndex2 = toppings.lastIndexOf('Avocado');
// console.log(avoIndex2);


// Check if hot sauce is in the toppings with includes()
const isInToppings = toppings.includes('Hot Sauce');
// console.log(isInToppings);
// add it if it's not
if (!isInToppings) {
toppings.push('Hot Sauce')
}
// console.log(toppings);

// flip those toppings around with reverse()

toppings.reverse();
// console.log(toppings); 

Arrays 2 : Static Methods Array.of() Array.from() Array.isArray() Object.entries() Object.keys() Object.value()

Array.of() Array.from() Array.isArray() Object.entries() Object.keys() Object.value()


// Array.of(); // Make an array from its arguments

// Array.of('Wes', 'Kait');
// Make an array from a String or Iterable.

// How to create a Range from x to y:

function createRange(start, end) {
const range = Array.from({ length: end - start + 1}, function(_item, index) {
return index + start;
})
return range;
}

// Make a function that creates a range from x to y with Array.from();
const myRange = createRange(3, 7424);

// Check if the last array you created is really an array with Array.isArray();
console.log(Array.isArray(myRange));

// Take the meats object and make three arrays with Object.entries(), Object.keys, Object.values()

// console.log(Object.entries(meats));
// console.log(Object.keys(meats));
// console.log(Object.values(meats));

//This one is already destructured:
Object.entries(meats).forEach(([key, value]) => {
// const key = entry[0];
// const value = entry[1];
// const [key, value] = entry;
 console.log(key, value);
});

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);

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

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