0

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": "[email protected]"
      }
    ]
  }
]

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

and I am getting this as result:

enter image description here

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": "[email protected]"
      }
    ]
  }
]
0

4 Answers 4

4

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":"[email protected]"}]}]

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Works like magic! Nice. I really need to loop and use spread to remain those data. Thanks, new knowledge for today.
@bossajie Glad I could help. BTW you might want to consider flattening your redux state to avoid nesting updates. Or there is a library immer that makes the update code to look more imperative.
I am using this before sending to the API, because the API accepts group_id only, not object, and in my frontend using some 3rd party component, I need group object to display a proper one, that's why I am doing this. Is that good practice?
I believe it is, unless you have a way to change API or frontend to match one another :)
0
  {
    "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": "[email protected]"
      }
    ]
  }
]

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.

Comments

0
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: "[email protected]"
  }]
}];
songs.map(({ performers }, ind) => {
 performers.map(({ group }, ind) => {
  performers[ind].group_id = group.group_id;
 });

 delete performers[ind].group;
});

console.log(songs);

Comments

-1

Use delete keyword: delete performerce ["keyname"] It will return the Boolean value

1 Comment

Assuming this question has redux tag mutating state is not an option.

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.