0

I have:

const dictionary  = [
  {
    "state": "AK",
    "lat": "9875.33.00",
    "long": "-8371.42.00",
    "name": "Alaska"
  },
  {
    "state": "AL",
    "lat": "5335.51.00",
    "long": "-15124.18.00",
    "name": "Alabama"
  }
];

const data = [
  {
    "date": 20200421,
    "state": "AK",
  },
  {
    "date": 20200421,
    "state": "AL",
  }
];

const result = data.map(item => ({...item, lat: dictionary[item.state].lat, long: dictionary[item.state].long }))
console.log(result);

Basically trying to add dictionary as objs per each data where the state matches but I'm having:

Cannot read property 'lat' of undefined

Expecting:

const result = [
  {
    "date": 20200421,
    "state": "AK",
    "lat": "9875.33.00",
    "long": "-8371.42.00",
  },
  {
    "date": 20200421,
    "state": "AL",
    "lat": "5335.51.00",
    "long": "-15124.18.00",
  }
];

I'm trying on fiddle

rob.m
  • 9,843
  • 19
  • 73
  • 162

3 Answers3

1

You could take an object with state as key and merge new objects by taking name out of the object.

const
    dictionary  = [{ state: "AK", lat: "9875.33.00", long: "-8371.42.00", name: "Alaska" }, { state: "AL", lat: "5335.51.00", long: "-15124.18.00", name: "Alabama" }],
    data = [{ date: 20200421, state: "AK" }, { date: 20200421, state: "AL" }],
    states = data.reduce((r, o) => (r[o.state] = o, r), {}),
    merged = dictionary.map(({ name, ...o }) => ({ ...(states[o.state] || {}), ...o }));

console.log(merged);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • thanks works perfectly, but when i try to load it vis an url it breaks. Not sure, is it an async issue you reckon? https://jsfiddle.net/51qyzwh2/ – rob.m Apr 22 '20 at 08:13
  • sorry this one, was trying the other answer https://jsfiddle.net/8rsbap4j/ – rob.m Apr 22 '20 at 08:16
  • `array` is not known. maybe renaming to `dictionary` would help. – Nina Scholz Apr 22 '20 at 08:18
  • sorry I'm switching in between the answers while trying and getting confused, now trying the other answer but I get lat is not defined not sure what I'm doing wrong, https://jsfiddle.net/7akxoqh1/ – rob.m Apr 22 '20 at 08:26
  • there is nothing wrong with your answer and actually I would have wanted to just accept it, i'm going around something stupid, I'm sure, the other answer told me I'm not loading data but I thought I was since I've set the url https://jsfiddle.net/7akxoqh1/ – rob.m Apr 22 '20 at 08:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212241/discussion-between-rob-m-and-nina-scholz). – rob.m Apr 22 '20 at 08:37
  • is it possible we have an issue with async? https://jsfiddle.net/a6dfhsz3/ – rob.m Apr 22 '20 at 08:40
  • bingo! the other answer resolved it https://jsfiddle.net/bL43g7sc/ – rob.m Apr 22 '20 at 08:42
1

You cannot access the array items as one whole array. You have to isolate you desired obj. Use this.

    const result = data.map(function(item) {
  const ditem = dictionary.find(d => d.state == item.state);
  if(ditem) {
  return {
    ...item,
    "lat": ditem.lat ,
    "long": ditem.long
  } 
  }
  return item;

});

console.log(result);
Rahul Purohit
  • 540
  • 4
  • 16
0

 const dictionary  = [
        {
      "state": "AK",
      "lat": "9875.33.00",
      "long": "-8371.42.00",
      "name": "Alaska"
     },
     {
      "state": "AL",
      "lat": "5335.51.00",
      "long": "-15124.18.00",
      "name": "Alabama"
     }
      ];
      
const data = [
  {
    "date": 20200421,
    "state": "AK",
  },
  {
    "date": 20200421,
    "state": "AL",
  }
];

let result = dictionary.filter(element =>{
   for(let i = 0; i < data.length; i++){
      if(data[i].state == element.state) return element;
   }
})
console.log(result);
bill.gates
  • 14,145
  • 3
  • 19
  • 47