0

I want to remove the key team_id from the following response:

{
  "id": 14,
  "name": "Angel Stadium of Anaheim",
  "opened": "1966-04-19T00:00:00.000Z",
  "capacity": 45517,
  "location": "Anaheim, California",
  "team_id": "95cbbe2a",
  "team": {
    "id": "95cbbe2a",
    "name": "Los Angeles Angels",
    "established_in": 1961,
    "league": "American League",
    "division": "West Division",
    "logo": "https://upload.wikimedia.org/wikipedia/commons/7/79/Los_Angeles_Angels_of_Anaheim_Insignia.svg",
    "number_of_titles":1
  }
}

The code I'm using right now gets all the data from the venues, at first I need all of them but at the final response I'd like to omit the the team_id key:

    const venues = await connection(VENUE_TABLE)
      .limit(LIMIT_PER_PAGE)
      .offset((page - 1) * LIMIT_PER_PAGE)
      .select('*')
      .orderBy('name');

    const venuesWithTeam = await venues.reduce(
      (promise, element) =>
        promise.then(async (result) =>
          result.concat({
            ...element,
            team: await connection('team')
              .where('id', element.team_id)
              .select('*')
              .first(),
          })
        ),
      Promise.resolve([])
    );

The desired behavior would be:

{
  "id": 14,
  "name": "Angel Stadium of Anaheim",
  "opened": "1966-04-19T00:00:00.000Z",
  "capacity": 45517,
  "location": "Anaheim, California",
  "team": {
    "id": "95cbbe2a",
    "name": "Los Angeles Angels",
    "established_in": 1961,
    "league": "American League",
    "division": "West Division",
    "logo": "https://upload.wikimedia.org/wikipedia/commons/7/79/Los_Angeles_Angels_of_Anaheim_Insignia.svg",
    "number_of_titles":1
  }
}

What can I do to make this happen?

  • `delete yourObject.team_id;` – Pointy May 07 '20 at 15:30
  • Does this answer your question? [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Mike K May 07 '20 at 15:36

3 Answers3

2

You can use object destructuring to remove specific keys from an object

const venueWithTeamId = {
  "id": 14,
  "name": "Angel Stadium of Anaheim",
  "opened": "1966-04-19T00:00:00.000Z",
  "capacity": 45517,
  "location": "Anaheim, California",
  "team_id": "95cbbe2a",
  "team": {
    "id": "95cbbe2a",
    "name": "Los Angeles Angels",
    "established_in": 1961,
    "league": "American League",
    "division": "West Division",
    "logo": "https://upload.wikimedia.org/wikipedia/commons/7/79/Los_Angeles_Angels_of_Anaheim_Insignia.svg",
    "number_of_titles":1
  }
};

const { team_id, ...withoutTeamId } = venueWithTeamId;
console.log(withoutTeamId);
ikarasz
  • 253
  • 2
  • 10
1

Use Object.assign to create a new object from existing one and then use delete to delete the key

let data = {
  "id": 14,
  "name": "Angel Stadium of Anaheim",
  "opened": "1966-04-19T00:00:00.000Z",
  "capacity": 45517,
  "location": "Anaheim, California",
  "team_id": "95cbbe2a",
  "team": {
    "id": "95cbbe2a",
    "name": "Los Angeles Angels",
    "established_in": 1961,
    "league": "American League",
    "division": "West Division",
    "logo": "https://upload.wikimedia.org/wikipedia/commons/7/79/Los_Angeles_Angels_of_Anaheim_Insignia.svg",
    "number_of_titles": 1
  }
}

let newData = Object.assign({}, data);
delete newData.team_id;
console.log(newData)
brk
  • 48,835
  • 10
  • 56
  • 78
0

You can just delete the property from the object

delete obj.team_id

We do this all the time with lodash / underscore available, so it's

return _.omit(obj, 'team_id')

Which allows you to get the desired result without modifying the original.

Mike K
  • 601
  • 5
  • 8