To set Letter spacing in canvas element with JavaScript, we can use the JavaScript array’s reverse
and fine
methods.
For instance, we can write:
const fruits = [{
shape: 'round',
name: 'orange'
},
{
shape: 'round',
name: 'apple'
},
{
shape: 'oblong',
name: 'zucchini'
},
{
shape: 'oblong',
name: 'banana'
},
{
shape: 'round',
name: 'grapefruit'
}
]
const currentShape = 'round'
const fruit = fruits.slice().reverse().find(fruit => fruit.shape === currentShape);
console.log(fruit)
We have the fruits
array.
And we want to find the last element that has the shape
property set to 'round'
.
To do this, we call fruits.slice
to return a copy of the fruits
array.
Then we call reverse
to reverse the array copy.
And finally, we call find
with a callback that checks if fruit.shape
is equal to currentShape
.
Therefore, fruit
is:
{shape: "round", name: "grapefruit"}
as we expect.