最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert Array to Map Typescript - Stack Overflow

programmeradmin4浏览0评论

I'm trying to covert the following array to a Map:

const arr = [
  { key: 'user1', value: { num: 0, letter: 'a' } },
  { key: 'user2', value: { num: 0, letter: 'b' } },
  { key: 'user3', value: { num: 0, letter: 'c' } },
];

What I have so far:

const arr = [
  { key: 'user1', value: { num: 0, letter: 'a' } },
  { key: 'user2', value: { num: 0, letter: 'b' } },
  { key: 'user3', value: { num: 0, letter: 'c' } },
];
const b = arr.map(obj => [obj.key, obj.value]);
const map = new Map<string, { num: number; letter: string }>(b);

console.log(map.get('user1'));

Do you know if this is something achievable?

PS: You can find Typescript playground here and the error that I'm getting

I'm trying to covert the following array to a Map:

const arr = [
  { key: 'user1', value: { num: 0, letter: 'a' } },
  { key: 'user2', value: { num: 0, letter: 'b' } },
  { key: 'user3', value: { num: 0, letter: 'c' } },
];

What I have so far:

const arr = [
  { key: 'user1', value: { num: 0, letter: 'a' } },
  { key: 'user2', value: { num: 0, letter: 'b' } },
  { key: 'user3', value: { num: 0, letter: 'c' } },
];
const b = arr.map(obj => [obj.key, obj.value]);
const map = new Map<string, { num: number; letter: string }>(b);

console.log(map.get('user1'));

Do you know if this is something achievable?

PS: You can find Typescript playground here and the error that I'm getting

Share Improve this question edited Jan 30, 2023 at 22:04 Ricardo Rocha asked Jan 30, 2023 at 21:58 Ricardo RochaRicardo Rocha 16.2k23 gold badges84 silver badges139 bronze badges 2
  • If you do it all in one line TypeScript infers the correct type playground const map = new Map(arr.map(obj => [obj.key, obj.value])); – pilchard Commented Jan 30, 2023 at 22:08
  • 1 const map = new Map(arr.map(obj => [obj.key, obj.value])); – Welcor Commented Jan 30, 2023 at 22:09
Add a comment  | 

1 Answer 1

Reset to default 19

obj.key is a string, and obj.value is a {num: number, letter: string}, so if you make an array of them ([obj.key, obj.value]), then you get an Array<string | {num: number, letter: string}>.

You need to tell TypeScript that you're creating a specific tuple, as expected by the Map constructor, not a generic array. There are a few ways of doing this:

// The simplest: Tell TypeScript "I mean exactly what I said."
// (This makes it readonly, which isn't always desirable.)
const b = arr.map(obj => [obj.key, obj.value] as const);

// Or be explicit in a return type. You can do this at a few levels.
const b = arr.map<[string, { num: number, letter: string}]>(obj => [obj.key, obj.value]);
const b = arr.map((obj): [string, { num: number, letter: string}] => [obj.key, obj.value]);

// Or, as explained in the comments, do it in one line, and TypeScript
// can infer tuple vs. array itself.
const map = new Map(arr.map(obj => [obj.key, obj.value]));
发布评论

评论列表(0)

  1. 暂无评论