Reputation: 578
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?
Upvotes: 4
Views: 324
Reputation: 4199
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.
Upvotes: 4
Reputation: 144
const dataInObject = {}; // Your data here
const dataArray = Object.keys(dataInObject).map(index=>dataInObject[index]);
console.log(dataArray);
Upvotes: 0