javascript 如何将一个对象的所有属性Map到另一个更大的对象?

bsxbgnwa  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(211)

我试着把一个对象数组Map到另一个对象数组,就像这样。

//array declarations   
originals!: Original[];
reviews!= Review[]; //some logic fills this array

//mapping logic below
this.originals = this.reviews.map(x => ({
a: x.a_1,
b: x.b_1
//if I don't set the remaining properties
//I am getting compiler errors so I am forced to do something like 
//the following, I don't want to do this.
c: "",
d: "" //and so on ..
});

originalreview对象具有以下属性。

Original {
    a: string,
    b: string,
    c: string,
    d: string,
    e: string        
}

Review {
    a_1: string,
    b_1: string      
}

问题-如何避免填充对象Original的所有剩余属性?

0lvr5msh

0lvr5msh1#

听起来您只需要可选属性:

type Original = {
    a: string,
    b: string,
    c?: string,
    d?: string,
    e?: string        
}

type Review = {
    a_1: string,
    b_1: string      
}

cde现在具有类型string | undefined

相关问题