0

I have the following array of objects:

const datasubject = 
[
 0: {
    level: "standard 3"
    subject: "English"
    _id: xxx
    coreCompetencies {
       0gHq0U5E667L4EdGbdZ2h: "Grammar",
       9CfalSpzKYIV7AaWKBUwg: "Listening",
       9boIfWUEGdj3WGxJL12XB: "Reading",
       QZ11uYQ8CXkRk0LWenjqj: "Writing",
       ZG1gtxRg6quIOYaTr6CUy: "Speaking"
    }
 },
 1: {...},
 2: {...}
]

I would like to change the coreCompetencies into an array of values e.g. ["Grammar", "Listening", "Reading", "Writing", "Speaking"] while preserving the other data. I've tried .map but got confused as it only returns the coreCompetencies object, without the other details. This is what I did that returned only the coreCompetencies object:

const datacore = datasubject.map(value => value.coreCompetencies);

I would like to achieve something like this:

const datasubject = 
[
 0: {
    level: "standard 3"
    subject: "English"
    _id: xxx
    coreCompetencies ["Grammar","Listening","Reading","Writing","Speaking"]
 },
 1: {...},
 2: {...}
]

2 Answers 2

4
const datacore = datasubject.map(({coreCompetencies, ...rest})=> {
  return {...rest, coreCompetencies: Object.values(coreCompetencies)}
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much @tam-dc! This works, and I'm using your solution because I can see where the 'rest' are.
A better implementation :)
2

You should do something like this :

const datacore = datasubject.map(value => {
  value.coreCompetencies = Object.values(value.coreCompetencies);
  return value;
})

1 Comment

Thanks @binariedme ! i've also tried your solution and it works! Super awesome!

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.