Brought to you by
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
myArray.forEach((element, index) => {
console.log(index, ': ', element.name);
});
0: John Snow
1: Daenerys Targaryen
2: Arya Stark
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
myArray.forEach(({ name }, index) => {
console.log(index, ': ', name);
});
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const anotherArray = [];
myArray.forEach(({ name }) => {
anotherArray.push(name);
});
return anotherArray;
You may be tempted to....
const foundElement = {}
myArray.forEach((element) => {
if (element.id === 234) {
foundElement = element;
}
});
return foundElement;
You may be tempted to....
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const john = myArray.find(({ name }) => name === 'John Snow');
console.log(john);
{ name: 'John Snow', id: 234 }
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const exists = myArray.find(({ name }) => name === 'John Snow');
if (exists) {
// do stuff
}
You may be tempted to....
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const exists = myArray.some(({ name }) => name.endsWith('Stark'));
console.log(exists);
true
.endsWith support:
(ES6)
[
{ id: 542, name: 'Tony Stark' },
{ id: 756, name: 'Arya Stark' }
]
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Tony Stark' },
{ id: 756, name: 'Arya Stark' },
];
const starks = myArray.filter(({ name }) => {
return name.endsWith('Stark');
});
console.log(starks);
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const names = myArray.map(({ name }) => name);
console.log(names);
[
'John Snow',
'Daenerys Targaryen',
'Arya Stark',
]
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Daenerys Targaryen' },
{ id: 756, name: 'Arya Stark' },
];
const names = myArray.map(({ name }) => {
const n = name.split(' ');
return {
firstName: n[0],
lastName: n[1],
};
});
console.log(names);
[
{ firstName: 'John', lastName: 'Snow' },
{ firstName: 'Daenerys', lastName: 'Targaryen' },
{ firstName: 'Arya', lastName: 'Stark' },
]
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Tony Stark' },
{ id: 756, name: 'Arya Stark' },
];
const starkNames = myArray
.filter(({ name }) => name.endsWith('Stark')
.map(({ name } => name);
You may be tempted to....
const myArray = [
{ id: 234, amount: 1 },
{ id: 542, amount: 4 },
{ id: 756, amount: 6 },
];
const total = myArray.reduce((result, { amount }) => {
result += amount;
return result;
}, 0);
console.log(total);
11
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Tony Stark' },
{ id: 756, name: 'Arya Stark' },
];
const starks = myArray.reduce((result, { name }) => {
if (name.endsWith('Stark')) {
const n = names.split(' ');
result.push({ firstName: n[0], lastName: n[1] });
}
return result;
}, []);
console.log(starks);
[
{ firstName: 'Tony', lastName: 'Stark' },
{ firstName: 'Arya', lastName: 'Stark' }
]
const myArray = [
{ id: 234, name: 'John Snow' },
{ id: 542, name: 'Tony Stark' },
{ id: 756, name: 'Arya Stark' },
];
const starks = myArray.filter(({ name }) => name.endsWith('Stark'))
.map(({ name }) => {
const n = name.split(' ');
return { firstName: n[0], lastName: n[1] };
});
console.log(starks);
[
{ firstName: 'Tony', lastName: 'Stark' },
{ firstName: 'Arya', lastName: 'Stark' }
]
See if an array of payments contains a payment over $100
.some()
Display the results of a search
.filter()
Add up the total amount paid from an array of payments
.reduce()
Display all payments over $100
.filter()
From a list of synced accounts, get the user's Gmail account
.find()
Display dates in an array in moment format rather than system time
.map()
Send only the ids of a list of contacts to the backend
.map()
Display only late invoices
.filter()
Display only contacts added in the last week
.filter()
Display whether or not there were contacts added in the last week
.some()