Home > Back-end >  Destructuring object with inner array without all keys
Destructuring object with inner array without all keys

Time:03-06

I have an object like this:

const objBefore: 
{
    "id": "3pa99f64-5717-4562-b3fc-2c963f66afa1",
    "number": "5000",
    "enabled": true,
    "classes": [
        {
            "id": "2fc87f64-5417-4562-b3fc-2c963f66afa4",
            "name": "General"
        },
        {
            "id": "7ffcada8-0215-4fb0-bea9-2266836d3b18",
            "name": "Special"
        },
        {
            "id": "6ee973f7-c77b-4738-b275-9a7299b9b82b",
            "name": "Limited"
        }
    ]
}

Using es6, I want to grab everything in the object except the name key of the inner classes array to pass it to an api.

So:

{
    "id": "3pa99f64-5717-4562-b3fc-2c963f66afa1",
    "number": "5000",
    "enabled": true,
    "classes": [
        {"id": "2fc87f64-5417-4562-b3fc-2c963f66afa4"},
        {"id": "7ffcada8-0215-4fb0-bea9-2266836d3b18"},
        {"id": "6ee973f7-c77b-4738-b275-9a7299b9b82b"}
    ]
}

The closest I got was: let {id, number, enabled, classes: [{id}]} = objBefore;

But it only gets me one id in classes. I've tried spreading above using [...{id}] or [{...id}]. Same thing.

I find it challenging to get the right mental model for how to think about this when it's on multiple levels. In my mind, when I say [...{id}] I'm thinking, "I want the id property as an object in the outer classes array, but give me every id in the array!"

Clearly I'm not thinking about this correctly.

I've tried it using map to get that part but I'm still having trouble combining it back to the original to produce the desired result. for example:

let classIds = objBefore.classes.map(({id}) => {
    return {
        id 
    }
})

(Using the map syntax, how can I destructure in the function the other keys that are one level higher?)

To combine them I started trying anything and everything, :

let {id, number, enabled, classIds} = {objBefore, [...classIds]} // returns undefined for all

I'd prefer to do it in one statement. But if that's not possible, then what's a clean way to do it using map?.

CodePudding user response:

You can't destructure and map at the same time in the way you're looking to do it. The main purpose of destructuring assignment is to extract data from an array/object and not for manipulating data. In your case, as you're after an object with the same keys/value as your original object, just with a different classes array, I would instead suggest creating a new object and spreading ... the original object into that. Then you can overwrite the classes array with a mapped version of that array:

const objBefore = { "id": "3pa99f64-5717-4562-b3fc-2c963f66afa1", "number": "5000", "enabled": true, "classes": [ { "id": "2fc87f64-5417-4562-b3fc-2c963f66afa4", "name": "General" }, { "id": "7ffcada8-0215-4fb0-bea9-2266836d3b18", "name": "Special" }, { "id": "6ee973f7-c77b-4738-b275-9a7299b9b82b", "name": "Limited" } ] };

const newObj = {
  ...objBefore,
  classes: objBefore.classes.map(({id}) => ({id}))
};
console.log(newObj);

CodePudding user response:

How about using simple util method with object destructuring, spread operator and map

const objBefore = {
  id: "3pa99f64-5717-4562-b3fc-2c963f66afa1",
  number: "5000",
  enabled: true,
  classes: [
    {
      id: "2fc87f64-5417-4562-b3fc-2c963f66afa4",
      name: "General",
    },
    {
      id: "7ffcada8-0215-4fb0-bea9-2266836d3b18",
      name: "Special",
    },
    {
      id: "6ee973f7-c77b-4738-b275-9a7299b9b82b",
      name: "Limited",
    },
  ],
};

const process = ({ classes, ...rest }) => ({
  ...rest,
  classes: classes.map(({ id }) => ({ id })),
});

console.log(process(objBefore))

CodePudding user response:

In one line, you could do this:

const objAfter = { ...objBefore, classes: objBefore.classes.map(item => ({ id: item.id })) };

Or, if you prefer:

const objAfter = {...objBefore, classes: objBefore.classes.map(({id}) => ({id}))};

There isn't any way in object destructing to copy an entire array of objects into a different array of objects by removing properties so you use .map() for that.

  • Related