Useful Javascript methods Flashcards
Use this: https://nextjs.org/learn-pages-router/foundations/from-javascript-to-react/essential-javascript-react
Using destructuring, load two variables, make and year, from the object car1.
Assume that car1 has properties with the same named variables.
const{make, year} = car1
Destructure an array ‘myArray’, placing the first two elements into variables var1 and var 2
const[var1, var2] = myArray
Inside a div, use a javascript method to take an array of objects named ‘skills’, and write out repeated set of components named ‘Skill’, which name property equal to the skillName property of each object in the array.
<div>{skills.map((skill) => <Skill name={skill.skillName}/>)}</div>
To a constant named result, assign the list of words from the array ‘words’ whose length is greater than 6.
const result = words.filter((word) => word.length > 6);
From an object array ‘cars’, put the first object into a new variable firstCar, and the remainder in a new array ‘remainingCards’.
const [firstCar, ...remainingCars] = cars;
Put the property ‘model’ of object ‘car1’ into a variable named ‘model’, and all the other properties of car1 into a new variable ‘otherInfo’
const {model, …otherInfo} = car1;
Define a new array ‘newCars’ by appending the object ‘car4’ on to the end of array ‘cars’.
const newCars = [...cars, car4];
Get some data from an API of url ‘myApiUrl’ and put it into an object named ‘data’
`
const result = await fetch(“https://myApiUrl”);
const data = await result.json();
`
Convert this code into an arrow function:
`
(function (a) {
return a + 100;
});
`
a => a + 100;
Convert this code into an arrow function:
~~~
(function (a, b) {
return a + b + 100;
});
~~~
() => a + b + 100;
Conver this function into an arrow function:
~~~
(function (a, b) {
const chuck = 42;
return a + b + chuck;
});
~~~
(a, b) => { const chuck = 42; return a + b + chuck; };
Define an immediately invoked, trad function, that has nothing but a comment as the function body.
(function() { /* */ })()
Define an immediately invoked, arrow function, that has nothing but a comment as the function body.
(() => { /* */ })()
Define an immediately invoked, arrow function, that returns ‘foobar’.
(() => "foobar")();
Define an arrow function entitled ‘simple’ that takes a parameter ‘a’, and returns 15 if ‘a’ is greater than 15, otherwise returns ‘a’.
const simple = (a) => (a > 15 ? 15 : a);