Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const animals = [
- {
- "name": "cat",
- "size": "small",
- "weight": 5
- },
- {
- "name": "dog",
- "size": "small",
- "weight": 10
- },
- {
- "name": "lion",
- "size": "medium",
- "weight": 150
- },
- {
- "name": "elephant",
- "size": "big",
- "weight": 5000
- }
- ];
- // Map - do same operation on each array element, return an array of same size
- animals.map(animal => animal.name);
- // ["cat", "dog", "lion", "elephant"]
- // Filter - return array that matching certain criteria
- animals.filter(animal => animal.size === "small");
- /*
- [
- {
- "name": "cat",
- "size": "small",
- "weight": 5
- },
- {
- "name": "dog",
- "size": "small",
- "weight": 10
- }
- ]
- */
- // Reduce - use the values in an array and return something completely new
- animals.reduce((weight, animal) => {return weight += animal.weight}, 0);
- // 5165
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement