Home > other >  How can i strip unwanted keys and containers from my array of objects
How can i strip unwanted keys and containers from my array of objects

Time:02-08

I have an array of objects that i need in a particular format. Currently it contains it's nested in such a way that it contains unwanted keys and container objects.

badArray = [
{
    "college": {
        "id": 1,
        "location": "Victoria",
        "rating": 10,
        "alumni": [
            {
                "alumni_id": 1,
                "alumni_position": 1
            },
            {
                "alumni_id": 2,
                "alumni_position": 3
            },
        ]
    }
},
{
    "college": {
        "id": 2,
        "location": "New York",
        "rating": 9,
        "alumni": [
            {
                "alumni_id": 5,
                "alumni_position": 7
            }
        ]
    }
}
]

What I'd like to get to is a less nested object with the following structure

goodArray = [
{
"id": 1,
"location": "Victoria",
"rating": 10,
"alumni": [
        {
         "alumni_id": 1,
         "alumni_position": 1
        },
        {
         "alumni_id": 2,
         "alumni_position": 3
        },             
    ]   
},
{   
"id": 2,
"location": "New York",
"rating": 9,
"alumni": [
        {
         "alumni_id": 5,
         "alumni_position": 7
        }
    ]
}
]

I can remove the unwanted key using

 Object.values(badArray[0])

But I'm really struggling to find a way to remove the unwanted outer container aswell.

Any help REALLY appreciated

CodePudding user response:

You can just do an array map and return your desired array in this situation instead of trying to delete keys

badArray = [
{
    "college": {
        "id": 1,
        "location": "Victoria",
        "rating": 10,
        "alumni": [
            {
                "alumni_id": 1,
                "alumni_position": 1
            },
            {
                "alumni_id": 2,
                "alumni_position": 3
            },
        ]
    }
},
{
    "college": {
        "id": 2,
        "location": "New York",
        "rating": 9,
        "alumni": [
            {
                "alumni_id": 5,
                "alumni_position": 7
            }
        ]
    }
}
]

let goodArray = badArray.map(el =>  el.college)
console.log(goodArray)

CodePudding user response:

Logic

  • Array.map through array.
  • Take Object.values of each Object in the array
  • Return the first value.

I have considered you have only one key in each object of the array. This solution will work for any valye for your key.

Working Fiddle

const badArray = [
  {
    college: {
      id: 1, location: "Victoria", rating: 10,
      alumni: [
        { alumni_id: 1, alumni_position: 1 },
        { alumni_id: 2, alumni_position: 3 },
      ],
    },
  },
  {
    college: {
      id: 2, location: "New York", rating: 9,
      alumni: [
        { alumni_id: 5, alumni_position: 7 },
      ],
    },
  },
];

const goodArray = badArray.map(item => Object.values(item)[0]);
console.log(goodArray);

If your key is constant as "college" you can perform this as.

const goodArray = badArray.map(item => item.college);
  •  Tags:  
  • Related