简体   繁体   中英

Remove object key and value inside of Array of Objects

My code =

This is my example data.

const songs = [
  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group": {
          "group_id": "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
          "dept_short_name": "PAO",
          "dept_long_name": "Public Affairs Office"
        },
        "email": "john@doe.com"
      }
    ]
  }
]

    const newTest= songs.map(( {...song} ) => (
        song.performers.map(({...group}) => group_id = group.group_id)
    ))

and I am getting this as result:

在此处输入图片说明

I should just remove the dept_short_name and dept_long_name from group object and the group_id will be remain, and the rest of the data of the songs should be remained the same.

This should be the result:

[
  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group_id":"1e5f73fa-ffe8-4c70-a83b-84e7bf985b25"
        "email": "john@doe.com"
      }
    ]
  }
]

You need to recreate an object in both of your maps.

 const songs = [{"title":"Just Once","asset_id":"1f7e0fd8-db21-4c28-b9e1-eb0295af198c","sort":1,"performers":[{"last_name":"John","first_name":"Doe","group":{"group_id":"1e5f73fa-ffe8-4c70-a83b-84e7bf985b25","dept_short_name":"PAO","dept_long_name":"Public Affairs Office"},"email":"john@doe.com"}]}] const updated = songs.map(({ performers, ...song }) => ({ ...song, // recreate song object performers: performers.map(({ group: { group_id }, ...performer }) => ({ ...performer, // recreate performer object group_id })) })) console.log(updated)

  {
    "title": "Just Once",
    "asset_id": "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
    "sort": 1,
    "performers": [
      {
        "last_name": "John",
        "first_name": "Doe",
        "group": {
          "group_id": "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
          "dept_short_name": "PAO",
          "dept_long_name": "Public Affairs Office"
        },
        "email": "john@doe.com"
      }
    ]
  }
]

window.addEventListener('load', function() {
 delete songs[0].performers[0].group.dept_short_name;
 delete songs[0].performers[0].group.dept_long_name;
 console.log(JSON.stringify(songs))
});

This may help.

const songs = [{
  title: "Just Once",
  asset_id: "1f7e0fd8-db21-4c28-b9e1-eb0295af198c",
  sort: 1,
  performers: [{
    last_name: "John",
    first_name: "Doe",
    group: {
      group_id: "1e5f73fa-ffe8-4c70-a83b-84e7bf985b25",
      dept_short_name: "PAO",
      dept_long_name: "Public Affairs Office"
    },
    email: "john@doe.com"
  }]
}];
songs.map(({ performers }, ind) => {
 performers.map(({ group }, ind) => {
  performers[ind].group_id = group.group_id;
 });

 delete performers[ind].group;
});

console.log(songs);

使用delete关键字:delete performerce ["keyname"] 返回布尔值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM