1

I have to manipulate data in this wonderful language which is javascript. Until there I managed to achieve what I needed on my own but I reached my limits there.

It's quite hard to explain how my data is structured so let's make a schema. This is what I have :

    obj: {[key: string]: {name: string, type: string}[]} = 
    {
        "animals": [
          {name: "Louis", type: "dog"},
          {name: "John", type: "cat"},
          {name: "Jordan", type: "dog"},
        ]
        "cars" : [
          {name: "alpha", type: "ferrari"},
          {name: "beta", type: "ferrari"},
          {name: "charlie", type: "mercedes"},
        ]
     }

What I try to achieve is for each object, grouping the object in the list by type. This would look like this :

obj: {[key: string]: {[key: string]: {name: string, type: string}[]}} = 
{
    "animals": {
      "dog": [
          {name: "Louis", type: "dog"},
          {name: "Jordan", type: "dog"},
      ],
      "cat": [
          {name: "John", type: "cat"},
      ]
    }
    "cars" : {
      "ferrari": [
          {name: "alpha", type: "ferrari"},
          {name: "beta", type: "ferrari"},
      ],
      "mercedes": [
          {name: "charlie", type: "mercedes"},   
      ]
    }
 }

Do you have any idea how to achieve that ?

5
  • 1
    Sorry, you are supposed to provide your attempt and explain where you're stuck. "Do you have any idea how to do what I want?" is not supposed to be a valid question for Stackoverflow. If you have tried something (with things like Object.keys, .map(), etc.) you should edit your question and add your code, so we can help you improve it. Commented Nov 25, 2021 at 15:01
  • 1
    @JeremyThille To be honest I have no clue how to start achieving this, that's why I didnt precise it Commented Nov 25, 2021 at 15:15
  • I know, hence my comment. Somebody answered and wrote all the code for you, but this is not supposed to happen. Stackoverflow does not accept "I have no idea how to do it, please write my code for me" questions. You are lucky it wasn't closed :) If you have no idea how to do something, in this case manipulate an object, you are encouraged to follow some tutorials instead. They are all over the place. Commented Nov 25, 2021 at 15:20
  • 1
    It's not like I can spend many days working on it while someone which is expert in js data structures can help me in a few minutes. I understand the logic of requiring a "generalised question" which can be helpful for other people, but here it's pretty hard to explain something which is a very particular case in a language I barely master. SO also a platform for helping people and some people should remember this ... :) Commented Nov 25, 2021 at 15:28
  • I get your point but I didn't make Stackoverflow's rules. Also, "An expert can do this in just a few minutes" yes, exactly, that's why they are called axperts, and how they usually make a living: they don't write code and develop things for free. They have spent years learning how to do in minutes what you can't do, or need to days to do, and therefore writing full code from scratch shouldn't be done for free. But yes, I know, it's nice to help people for free, I get it. Commented Nov 25, 2021 at 17:02

1 Answer 1

3

I think you're looking for the following (seeing as you have TypeScript in the title of your question):

interface Value {
  name: string;
  type: string;
}

type Categorized = {
  [index: string]: Record<string, Value[]>;
};

const results = Object.entries(obj).reduce<Categorized>((mapping, [key, values]) => {
  const group: Record<string, Value[]> = {};

  for (const value of values) {
    if (group[value.type] === undefined) {
      group[value.type] = [value];
    } else {
      group[value.type].push(value);
    }
  }

  return {
    ...mapping,
    [key]: group,
  };
}, {});
Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly, thanks !
I would recommend to read documentation of frequently used functions such as .entries, .reduce .map, etc.
yes i will before implementing your code

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.