4

I have data coming from API, in a format like:

this.userSkills = [
    {
        skill_level: {
            skill: {
                id: 1,
                proficiency: "Beginner",
                name: "Core Java"
            }
        }
    },
]

I want to map them into objects to be like:

[
    {skillId: 1, skillProficiency: "Beginner", skillName: "Core Java"},
    {skillId: 7, skillProficiency: "Intermediate", skillName: "ReactJs"},
    {skillId: 2, skillProficiency: "Beginner", skillName: "Javascript"},
    {skillId: 27, skillProficiency: "Intermediate", skillName: "Common behavioral "},
    {skillId: 29, skillProficiency: "Beginner", skillName: "iOS"},
    {skillId: 34, skillProficiency: "Beginner", skillName: "API Testing"}
]

Which by using map operator I have tried to convert them into objects, like:

this.userSkills.map(value => { 
    const data = { 
        skillId: value.skill_level.skill.id, 
        skillProficiency: value.skill_level.proficiency, 
        skillName: value.skill_level.skill.name }; 
    const test = [] test.push(data); 
    console.log(test) 
});

like this, but I want them as an array of objects so that I can loop over them, how can I convert them into array of objects?

7
  • you may declare and empty array and use push method to form array of objects Commented Dec 26, 2019 at 7:15
  • I didn't get it; you already converted them into array by using map? Commented Dec 26, 2019 at 7:16
  • i tried but this.userSkills.map(value => { const data = { skillId: value.skill_level.skill.id, skillProficiency: value.skill_level.proficiency, skillName: value.skill_level.skill.name }; const test = [] test.push(data); console.log(test) }); [{…}] 0: {skillId: 1, skillProficiency: "Beginner", skillName: "Core Java"} length: 1 proto: Array(0) [{…}] [{…}] but this is how it is coming Commented Dec 26, 2019 at 7:17
  • @nimeresam i converted them into objects Commented Dec 26, 2019 at 7:20
  • Please Refer below link. Combined Multiple Object Into Array Commented Dec 26, 2019 at 7:24

2 Answers 2

4

As I understood from you; you tried this?

this.userSkills.map(value => { 
    const data = { 
        skillId: value.skill_level.skill.id, 
        skillProficiency: value.skill_level.proficiency, 
        skillName: value.skill_level.skill.name }; 
    const test = [] test.push(data); 
    console.log(test) 
});

Correct me if I'm wrong?

If yes, your problem is with test, because it's scooped inside map function.

map function already return a new converted array, check documentation.

try this:

this.newArr = this.userSkills.map(value => 
    ({ 
        skillId: value.skill_level.skill.id, 
        skillProficiency: value.skill_level.proficiency, 
        skillName: value.skill_level.skill.name
    })
);

And use newArr in ngFor, it should work.

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

Comments

0
const dataInObject = {}; // Your data here
const dataArray = Object.keys(dataInObject).map(index=>dataInObject[index]);
console.log(dataArray);

Comments

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.