# Looking up another array from d3 joined data

**URL:** <https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618>\
**Category:** Help\
**Created:** [September 26, 2021, 1:04pm UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618 "2021-09-26T13:04:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisWoodsSays](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/chriswoodssays/32/3754_2.png) [@ChrisWoodsSays](https://talk.observablehq.com/u/ChrisWoodsSays)\
**Post date:** [September 26, 2021, 1:04pm UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618/1 "2021-09-26T13:04:45Z")

</div>

I need to lookup a value in another array based on a key within the data joined to svg within d3.

The d3 joined dataset is `boundaries` (which contains a key of `lad17cd` for the local authority) and then I need to look up the population in `localAuthorities` using the same key.

I’ve tried various ways (using find on local authorities and the [] notation to refer to a key, but for that, I’m not sure how it would know which field is the key), but none seem to work.

Full code is at [Carbon Calculator Map / Chris Woods / Observable](https://observablehq.com/@chriswoodssays/carbon-calculator-map), with the lookup code towards the bottom of `leafletBoundaryMap`.

The currently uncommented line refers to a specific value of lad17cd and this works, but obviously I don’t want to hard code it.

Any pointers warmly welcomed.

```auto
const areaPaths = g.selectAll('path')
    .data(boundaries.features)
    .enter()
    .append('path')
    //.attr("fill", d => localAuthorities[d.properties.lad17cd].population > 100000 ? "#ffe26d" : "#c837ed")
    .attr("fill", localAuthorities.find(d => d.lad17cd === "E06000013").population < 100000 ? "#ffe26d" : "#c837ed")
    //.attr("fill", d => localAuthorities.find(la => la.lad17cd === d.properties.lad17cd).population < 100000 ? "#ffe26d" : "#c837ed")

```

---

<div class="post-metadata">

**Author:** ![hellonearthis](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/hellonearthis/32/3935_2.png) [@hellonearthis](https://talk.observablehq.com/u/hellonearthis)\
**Post date:** [September 27, 2021, 12:35am UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618/2 "2021-09-27T00:35:37Z")

</div>

I made a localIndex object from the from localAuthorities data.  
That lets me reference the array of localAuthorities using the key from d.properties.lad17cd

```auto
localIndex = {
  let idx = {}
  let idxlo = localAuthorities.map(d => d.lad17cd)
  for (let i = 0; i < idxlo.length; ++i) {
   idx[idxlo[i]]= i
  }
  return idx
}

```

```auto
    .attr("fill", d => localAuthorities[localIndex[d.properties.lad17cd]].population > 100000 ? "#ffe26d" : "#c837ed")

```

That shows now show a bug because d.properties.lad17cd is trying to find these data points and they are not present in localAuthorities

```nohighlight
S12000005,S12000006,S12000008,S12000010,S12000011,S12000013,S12000014,S12000015,S12000017,S12000018,S12000019,S12000020,S12000021,S12000023,S12000024,
S12000026,S12000027,S12000028,S12000029,S12000030,S12000033,S12000034,S12000035,S12000036,S12000038,S12000039,S12000040,S12000041,S12000042,S12000044,
S12000045,S12000046,W06000001,W06000002,W06000003,W06000004,W06000005,W06000006,W06000008,W06000009,W06000010,W06000011,W06000012,W06000013,W06000014,
W06000015,W06000016,W06000018,W06000019,W06000020,W06000021,W06000022,W06000023,W06000024

```

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 27, 2021, 9:34am UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618/3 "2021-09-27T09:34:11Z")

</div>

> [@hellonearthis](#):
>
> ```auto
> let idx = {}
> let idxlo = localAuthorities.map(d => d.lad17cd)
> for (let i = 0; i < idxlo.length; ++i) {
> idx[idxlo[i]]= i
> }
> 
> ```

You may want to use a Map for that:

```js
lad17cd = new Map(localAuthorities.map(d => [d.lad17cd, d]))

```

```js
lad17cd.get(d.properties.lad17cd).population
```

---

<div class="post-metadata">

**Author:** ![ChrisWoodsSays](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/chriswoodssays/32/3754_2.png) [@ChrisWoodsSays](https://talk.observablehq.com/u/ChrisWoodsSays)\
**Post date:** [September 27, 2021, 10:33am UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618/4 "2021-09-27T10:33:00Z")

</div>

Thanks @hellonearthis and @mootari. @mootari, am I right in thinking the map creates a new array of key value pairs, with the value being the whole object from the original array? And that you do this as the lookup only works with key value pairs?

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [September 27, 2021, 2:59pm UTC](https://talk.observablehq.com/t/looking-up-another-array-from-d3-joined-data/5618/5 "2021-09-27T14:59:54Z")

</div>

@ChrisWoodsSays While the keys in a Map can be arbitrary things, they only work for 1:1 relationships (in case of duplicate keys only the last entry is kept). If you want to create indexes for non-unique keys, I recommend taking a look at [d3.group and d3.rollup](https://observablehq.com/@d3/d3-group).
