如何使用打字稿将 string[] 数组添加到 record<string,boolean> 格式中?

Mik*_*hun 4 arrays typescript angular

我有一个字符串数组,我想保留这些值并创建一个新的数组记录

对于 userValue 中的每个值。

例子

 userValue: string[] = ["1111","2222","3333","4444"];


  selectedOptions: Record<string, boolean> = {
    //how to add userValue array into here?
    1111: true, //hardcoded
    2222: true, //hardcoded
    3333: true, //hardcoded
    4444: true //hardcoded
  };
Run Code Online (Sandbox Code Playgroud)

Mui*_*hdy 6

// Create the array of strings
let userValue: string[] = ["1111","2222","3333","4444"];

// Create empty Record
let selectedOptions: Record<string, boolean> = {} as Record<string, boolean>;

// Add the array values into the Record
userValue.forEach(val => {
   selectedOptions[val] = true;
});
Run Code Online (Sandbox Code Playgroud)