0

I'm trying to figure out how to create a lens that'll give me an array of the key values from within an array. Here's a simple example:

const L = require('partial.lenses');

const data = [
  {
    r: [
      {
        d: {
          one: 1,
          two: 2
        }
      }
    ]
  },
  {
    r: [
      {
        d: {
          three: 3,
          four: 4
        }
      }
    ]
  }
];

const lens = L.compose (
  L.elems,
  L.prop ('r'),
  L.elems,
  L.prop ('d'),
);

const result = L.get (lens, data);
console.log (result);

I want:

[{ one: 1, two: 2 }, { three: 3, four: 4 }]

But get:

{ one: 1, two: 2 }

I'm sure this is trivial, but can't get it quite right. Once my lens correctly selects the array of 'd's I want to use L.modify to get the data with all of the 'd' objects replaced with a string. I think I know how to do once my lens is correct.

Thanks

HypeXR
  • 711
  • 2
  • 6
  • 19

1 Answers1

2

Use L.collect instead of L.get, L.get returns the first found entity, while L.collect returns all matching entities, similar to [].filter vs [].find.

https://github.com/calmm-js/partial.lenses#l-collect

dotconnor
  • 1,736
  • 11
  • 22