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?

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
juhi
  • 558
  • 1
  • 10
  • 19
  • you may declare and empty array and use push method to form array of objects – Utkarsh Dec 26 '19 at 07:15
  • I didn't get it; you already converted them into array by using map? – Nimer Awad Dec 26 '19 at 07: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 – juhi Dec 26 '19 at 07:17
  • @nimeresam i converted them into objects – juhi Dec 26 '19 at 07:20
  • Please Refer below link. [Combined Multiple Object Into Array](https://stackoverflow.com/questions/2454295/how-to-concatenate-properties-from-multiple-javascript-objects) – Vishal Dec 26 '19 at 07:24
  • @Vishal I think he wants to map list of objects into another format – Nimer Awad Dec 26 '19 at 07:35
  • Not sure why this question got so many upvotes :-| – Adams Hales Dec 26 '19 at 08:41

2 Answers2

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.

Nimer Awad
  • 3,967
  • 3
  • 17
  • 31
0
const dataInObject = {}; // Your data here
const dataArray = Object.keys(dataInObject).map(index=>dataInObject[index]);
console.log(dataArray);
Arayik
  • 114
  • 2
  • 10