Using our functional utilities for retrieving deep nested properties.
Published: 2023-11-23 15:36:55

Let's see a practical case of our tiny [functional toolbox](https://dev.to/artydev/some-functional-utilities-3bah).

Here, the object we will use :

```js
let pets = [
  {
    petname : "Bill",
    breed : "poodle",
    weight: 12,
    owner : {
      ownername : "Paul",
      contribution : {
        amount : 32,
        payed : false
      },
      address : {
        city :  "Paris"
      }
    }
  },
  {
    petname : "Maya",
    race : "pointer",
    weight: 27,
    owner : {
      ownername : "Henri",
      contribution :  {
        amount : 12,
        payed : true
      },
      address : {
         city :  "London"
      }
    }
  },
  {
    petname : "Ooper",
    race : "setter",
    weight: 20,
    owner : {
      ownername : "Nicolas",
      contribution :  {
        amount : 12,
        payed : true
      },
      address : {
         city :  "London"
      }
    }
  }
]
```
Say you want the list of all the cities.

With our tools, we can create a function which, when given a 'string path' for a property, returns the property value.

Let's name this function **pick**.

Here is a possible implementation :

```js

function pick (path) {
  let pickprops =  map((prop) => pickprop(prop), path.split("."));
  return pipe(...pickprops);
}


```
We can now  create a function, which will return a 'city value' for a single entry.

```js

let pickCity = pick("owner.address.city");

```
Nice isn't it ?

Now we can explicitely list all the cities :

```js

let pickCity = pick("owner.address.city");


let cities = map(pickCity)(pets);

// or

cities =  map(pickCity, pets);

console.log(cities)

```

I like this one too :

```js

let cityMapper = map(pick("owner.address.city"));

let cities = cityMapper(pets);

console.log(cities)

```

We can go a step further and feel the spirit of functional programming : 

```js

let mapper = compose(map, pick);

let citymapper = mapper("owner.address.city")

cities = citymapper(pets)

console.log(cities)

```

Result :


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fm22jthn6kjpta5wnmhv.png)


You can play with a demo here [Demo](https://flems.io/#0=N4IgzgpgNhDGAuEAmIBcIBOBXARgS1gHsA6AKzBABoQAzPGC1AbVADsBDAWwjRGIAt4nKFRBFWiCbxABfSmy4902fETIVq4yfF4AdVjHgACAA4R4YIwF4jTfUaPB7D0+Y7cjqI7pAAhelA+lM4OOBgQyJ7eICaEhEgwQS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