Search⌘ K

Use With Arrays

Discover how to apply lenses to arrays in RamdaJS by using lensIndex instead of lensProp. Understand how this technique allows precise, functional manipulation of array data.

We'll cover the following...

What we just learned applies to arrays as well, but with one change. Instead of lensProp use lensIndex.

Javascript (babel-node)
import { lensIndex, view } from 'ramda';
const person = {
firstName: 'Bobo',
lastName: 'Flakes',
friends: [{
firstName: 'Clark',
lastName: 'Kent'
}, {
firstName: 'Bruce',
lastName: 'Wayne'
}, {
firstName: 'Barry',
lastName: 'Allen'
}]
};
const getThirdFriend = lensIndex(2);
const result = view(getThirdFriend, person.friends);
console.log({ result });