92174


Angularにて画面を作っています。ある画面のコンポーネントにて、

[code]

    class Sub {
        constructor(
            aaa: string,
            bbb: string,
            ccc: string
        ) {}
    }
    
[/code]

上記のクラスをimportし、この型でAPIからレスポンスが帰ってくるのでそれをTableの行選択で利用できるように

[code]

    type ExtendSub = Sub & {
        isSelected: boolean;
    }
    
    
    // 返ってきたレスポンス
    const response: Sub[] = [
        { aaa: "a", bbb: "b", ccc: "c" },
        { aaa: "a", bbb: "b", ccc: "c" },
        { aaa: "a", bbb: "b", ccc: "c" },
    ]
    
[/code]

これを下記のように追加がしたいのですが

[code]

    const ex: ExtendSub[] = [
        { aaa: "a", bbb: "b", ccc: "c", isSelected: false },
        { aaa: "a", bbb: "b", ccc: "c", isSelected: false },
        { aaa: "a", bbb: "b", ccc: "c", isSelected: false },
    ]
    
[/code]

どのように変換したら良いかがわかりません。これ以外に解決方法があればご教授いただけると幸いです。


CC BY-SA 4.0
2022-11-11T17:22:12.337
92169
2022-11-12T01:03:52.040
2022-11-12T01:03:52.040
7997
55338
question
typescript angular
TypeScriptの配列の詰替えがしたいです


[map関数](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/map)を使うと次のように書けます。

[code]

    type Sub = {
        aaa: string;
        bbb: string;
        ccc: string;
    }
    
    type ExtendSub = Sub & {
        isSelected: boolean;
    }
    
    const response: Sub[] = [
        { aaa: "a", bbb: "b", ccc: "c" },
        { aaa: "a", bbb: "b", ccc: "c" },
        { aaa: "a", bbb: "b", ccc: "c" },
    ]
    
    const ex = response.map<ExtendSub>(item => {
        return { ...item, isSelected: false }
    });
    
    console.log(ex); // 期待値
    
[/code]

[Playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAygrgIygXigbwFBW1AhvgLigGdgAnASwDsBzAbixwWaNMtoZygGNfXzq9DAF8MGUJCgBRAB7AIVACbwkqFVABk6RtgrEYEADYRu8xUQQB7S8dxUGojN0tVSUMhGJgXxCERUA2gC6KFABOuh4hFAARLgxADRQzAhEMQiJPHyx3DFQwgkRaFG4afFJKWkZSbzcabn5hVzF+KWx5ckssdVZdTl5BRhBYs6uwFAQMqEeXj4QAHQAtrhgADyy8koqAHwAFBTyiyjb2lwewHBkVJHztwcQi0l6BsamEOZQAGa4hr75IgBKEY+GwLQyWGi7SYAhhAA)


CC BY-SA 4.0
2022-11-12T01:02:26.540
92174
2022-11-12T01:02:26.540
7997
92169
answer