Skip to content

2794. Create Object from Two Arrays

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };

function createObject(
  keysArr: JSONValue[],
  valuesArr: JSONValue[]
): Record<string, JSONValue> {
  let ans: Record<string, JSONValue> = {};
  keysArr.forEach((key, index) => {
    let stringKey: string = typeof key === 'string' ? key : String(key);
    if (!(stringKey in ans)) {
      ans[stringKey] = valuesArr[index];
    }
  });
  return ans;
}