将对象转换成数组

时间:2019-12-26 07:13:01

标签: javascript angular typescript angular6

我有来自API的数据,格式如下:

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

我想将它们映射为对象,例如:

[
    {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"}
]

通过使用map运算符,我试图将它们转换为对象,例如:

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) 
});

像这样,但是我希望它们作为对象数组,以便可以在它们上循环,如何将它们转换为对象数组?

2 个答案:

答案 0 :(得分:4)

据我了解,你尝试过这个吗?

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) 
});

如果我写错了我吗?

如果是,则您的问题出在test上,因为它是在map函数中发现的。

map函数已经返回一个新的转换后的数组,请检查documentation

尝试一下:

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

并在newArr中使用ngFor,它应该可以工作。

答案 1 :(得分:0)

SELECT  MAX(scaleA.VAL) AS scaleA_val, MAX(scaleB.VAL) AS scaleB_val FROM RSDU2ELARH.EL008_6305119 scaleA 
FULL OUTER JOIN RSDU2ELARH.EL008_6305126 scaleB ON scaleA.time1970 = scaleB.time1970 
WHERE scaleA.time1970 > TO_DT1970(TO_DATE ('2019/11/01', 'yyyy/mm/dd')) AND scaleA.time1970 < TO_DT1970(TO_DATE ('2019/12/01', 'yyyy/mm/dd')) 
GROUP BY TO_CHAR(FROM_DT1970(scaleA.time1970), 'DD')
-- use this order by clause
Order by TO_CHAR(FROM_DT1970(scaleA.time1970), 'DD')