I am trying to combine reduce() with map() to reduce multiple box art objects to a single value: the url of the largest box art.
It gives me the following error :
VM272:20 Uncaught TypeError: boxarts.reduce(...).map is not a function at <anonymous>:20:4
Not sure what exactly is wrong
const boxarts = [
{ width: 200, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg" },
{ width: 150, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture150.jpg" },
{ width: 300, height: 200, url: "http://cdn-0.nflximg.com/images/2891/Fracture300.jpg" },
{ width: 425, height: 150, url: "http://cdn-0.nflximg.com/images/2891/Fracture425.jpg" }
]
let newReduce = boxarts.reduce(function(accumulator, currentValue) {
if (accumulator.width * accumulator.height > currentValue.width * currentValue.height) {
return accumulator
} else {
return currentValue
}
}).map(function(item) {
return item.url
})
console.log(newReduce)
SOLUTION to the initial question. It returns the link as an array of object.
const boxarts = [{
width: 600,
height: 700,
url: "http://cdn-0.nflximg.com/images/2891/Fracture600.jpg"
},
{
width: 500,
height: 500,
url: "http://cdn-0.nflximg.com/images/2891/Fracture500.jpg"
},
{
width: 425,
height: 150,
url: "http://cdn-0.nflximg.com/images/2891/Fracture425.jpg"
},
{
width: 200,
height: 200,
url: "http://cdn-0.nflximg.com/images/2891/Fracture200.jpg"
}
]
let newReduce = [boxarts.reduce(function(acc, currentValue) {
if (acc.width * acc.height > currentValue.width * currentValue.height) {
return acc
} else {
return currentValue
}
})].map(function(item) {
return {
url:item.url}
})
console.log(newReduce)
reduceandmap? What do you expect the result to be?