0

I'm trying to do an hashmap similar task here. Certainly feeling hard. USA is common for both Honda and Tesla, JAPAN for Honda and China for Hyundai. I've given an attempt here, but I feel like I'm overcomplicating things by doing map inside map.

const cars = [
  {
    id: 1,
    name: 'Honda',
    model: 2017,
    data: [
      { id: 101, city: 'JAPAN' },
      { id: 102, city: 'USA' },
    ],
  },

  {
    id: 2,
    name: 'Tesla',
    model: 2018,
    data: [
      { id: 103, city: 'CHINA' },
      { id: 102, city: 'USA' },
    ],
  },

  {
    id: 3,
    name: 'Hyundai',
    model: 2019,
  },
];

const obj = {};
const arr = [];

cars.map((item) => {
  if ('data' in item) {
    item.data.map((ele) => {
      obj.myCarId = {
        data: { id: ele.id, city: ele.city },
        cars: [{ id: item.id, name: item.name, model: item.model }],
      };
      arr.push(obj);
    });
  }
});

// Expected answer:
 {
 ['101']: {
    data:{ id: 101, city: 'JAPAN'},
    car:[{id: 1,name: 'Honda',model: 2017}],
    },
  ['102']: {
    data:{ id: 102, city: 'USA'},
    car:[{id: 1,name: 'Honda',model: 2017},{id: 2,name: 'Tesla',model: 2018}],
  },
 ['103']: {
     data:[{ id: 103, city: 'CHINA'}],
     car:[{id: 3, name: 'Hyundai',model: 2019}],  
  },
 }

How do I map my USA data with Honda and Tesla and produce output something like the above?

2
  • I think you can do this with flatMap. You would have to map inside it to return tuples with [id, data] and then finally do Object.fromEntries against the array. Commented Aug 12, 2022 at 10:54
  • your code would be better with a foreach instead of a map as you aren't actually mapping anything Commented Aug 12, 2022 at 11:25

1 Answer 1

1

you can use a flatMap to convert an array of arrays into an array then you can use Object.fromEntires to convert an array of [key,value] into an object

so then all you need to do is unwrap your data

EDIT: Looks like i misread your requirements so here is a new version using a map instead of an object and some for loops rather than callbacks

const cars = [
  {
    id: 1,
    name: 'Honda',
    model: 2017,
    data: [
      { id: 101, city: 'JAPAN' },
      { id: 102, city: 'USA' },
    ],
  },
  {
    id: 2,
    name: 'Tesla',
    model: 2018,
    data: [
      { id: 103, city: 'CHINA' },
      { id: 102, city: 'USA' },
    ],
  },
  {
    id: 3,
    name: 'Hyundai',
    model: 2019,
  },
];

const map = new Map();

for (const car of cars) {
  for (const datum of car?.data ?? []) {
    if (!map.has(datum.id)) {
      map.set(datum.id, { data: datum, cars: [] });
    }
    map.get(datum.id).cars.push({
      id: car.id,
      name: car.name,
      model: car.model,
    });
  }
}

console.log(Object.fromEntries(map.entries()));

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @mikeT. But I'm looking to group 102, so the car can be an array of objects. For ex: ['102']: { data:{ id: 102, city: 'USA'}, car:[{id: 1,name: 'Honda',model: 2017},{id: 2,name: 'Tesla',model: 2018}], } Do you recommend a way?
its would still work with a tweak to group the car entries before creating the object (i misread your expected output) though at that point it probably wouldn't be much better that your orginal code using the correct foreach instead of the map
@user2824374 updated the answer, to add the grouping, though most of the change from your orginal code are just updating your function to use new JS features

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.