This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let grouped = cars.reduce( | |
(groups, curr) => { | |
let key = curr.make + ',' + curr.colour; | |
if (groups[key]===undefined) { | |
groups[key] = {count:1, minPrice:curr.price, maxPrice: curr.price }; | |
} else { | |
groups[key].count++; | |
groups[key].minPrice = Math.min(groups[key].minPrice, curr.price); | |
groups[key].maxPrice = Math.max(groups[key].maxPrice, curr.price); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let grouped = cars.reduce( | |
(groups, curr) => { | |
let key = curr.make; | |
if (groups[key]===undefined) { | |
groups[key] = {count:1, minPrice:curr.price, maxPrice: curr.price }; | |
} else { | |
groups[key].count++; | |
groups[key].minPrice = Math.min(groups[key].minPrice, curr.price); | |
groups[key].maxPrice = Math.max(groups[key].maxPrice, curr.price); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let unique = cars.filter ( (item,index) => index === cars.findIndex ( car => car.make === item.make && car.model === item.model) ); | |
// RESULT IS: | |
[ | |
{id:1,make:"Ferrari",model:"812GTS",price:336000,colour:"rosso corsa"}, | |
{id:2,make:"Ferrari",model:"F8 Spider",price:262000,colour:"giallo modena"}, | |
{id:3,make:"Lamborghini",model:"Aventador S",price:329400,colour:"blu le mans"}, | |
{id:4,make:"Bugatti",model:"Chiron Pur Sport",price:3000000,colour:"blue"}, | |
{id:5,make:"McLaren",model:"New GT",price:203000,colour:"helios orange"} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let carsAsString = cars.map( (item) => JSON.stringify(item)); | |
let unique = [...new Set(carsAsString)].map( (item) => JSON.parse(item) ); | |
// RESULT IS: | |
[ | |
{id:1,make:"Ferrari",model:"812GTS",price:336000,colour:"rosso corsa"}, | |
{id:2,make:"Ferrari",model:"F8 Spider",price:262000,colour:"giallo modena"}, | |
{id:3,make:"Lamborghini",model:"Aventador S",price:329400,colour:"blu le mans"}, | |
{id:4,make:"Bugatti",model:"Chiron Pur Sport",price:3000000,colour:"blue"}, | |
{id:5,make:"McLaren",model:"New GT",price:203000,colour:"helios orange"}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cars.sort( (a,b) => a.make < b.make ? -1 | |
: a.make > b.make ? 1 | |
: a.price > b.price ? -1 | |
: a.price < b.price ? 1 | |
: 0 ); | |
// cars is now: | |
[ | |
{id:4,make:"Bugatti",model:"Chiron Pur Sport","price":3000000,colour:"blue"}, | |
{id:8,make:"Bugatti",model:"Chiron Pur Sport","price":3000000,colour:"red"}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maxPrice = cars.reduce( (prevPrice, current) => prevPrice < current.price ? current.price : prevPrice, 0); | |
let priceyCars = cars.filter( (car) => car.price==maxPrice ); | |
// RESULT IS: | |
[{ | |
id: 4, | |
make: "Bugatti", | |
model: "Chiron Pur Sport", | |
price: 3000000, | |
colour: "blue" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maxPrice = cars.reduce( (prevPrice, current) => prevPrice < current.price ? current.price : prevPrice, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maxPrice = cars.reduce( (prevPrice, current) => prevPrice < current.price ? current.price : prevPrice, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let maxPrice = Math.max(...cars.map(item => item.price)); | |
// RESULT IS: 3000000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let car = cars.reduce( (prev, current) => prev.price<current.price ? current:prev, {price:0}); |