选择使用Typescript从元组中进行选择。

huangapple go评论80阅读模式
英文:

Select from tuple with Typescript

问题

我正在完成一个课程,当前的挑战是“编写一个函数,该函数接受一个包含姓名和年龄的数组,然后仅返回姓名。”

到目前为止,我的方法看起来是这样的:
```typescript
function names(arr: [string, number][]) {
  let result: string[] = [];
  arr.forEach((element) => {
    if (typeof(element[0]) === "string") {
      result.push(element[0]);
    }
  })
  return result;
}

我试图使测试通过,测试断言我是否真的只从我的函数中返回了字符串。我感到困惑,我目前的解决方案导致以下错误:

names([['Amir', 34], ['Betty', 17]])

期望结果: ['Amir', 'Betty']但实际得到: 类型错误:类型 'string' 不能赋值给类型 'never' 的参数。

任何帮助都将不胜感激!


<details>
<summary>英文:</summary>

I&#39;m working through a course, and the current challenge is to &quot;Write a function that takes an array of names and ages, then returns only the names.&quot;

So far my approach has looked like this: 

function names(arr: [string, number]) {
let result: [] = [];
arr.forEach((element) => {
if (typeof(element) === "string") {
result.push(element);
}
})
return result;
}

I&#39;m trying to get tests to pass that assert that I am actually getting just the strings returned from my function.  I&#39;m at a loss, the current error I&#39;m getting from my attempt at a solution is 

names([['Amir', 34], ['Betty', 17]])

Expected: ['Amir', 'Betty'] but got: type error: Argument of type 'string' is not assignable to parameter of type 'never'.


Any help is greatly appreciated!

</details>


# 答案1
**得分**: 4

只需将以下部分翻译:

```javascript
let result: [] = [];

为:

let result: string[] = [];

此外,看起来您的参数是一个数组的数组。因此,您需要将以下部分定义为:

arr: [string, number]

arr: [string, number][]

或等效的语法为

arr: Array<[string, number]>

然后,也需要更改您的for循环。类似这样:

function names(arr: [string, number][]) {
  let result: string[] = [];
  arr.forEach(([element]) => {
    result.push(element);
  })
  return result;
}

请注意,我使用了解构赋值来获取第一个元素。

英文:

You just need to define

let result: [] = [];

as

let result: string[] = [];

Also looks like your parameter is an array of arrays. So you need to define

arr: [string, number]

as

arr: [string, number][]

or equivalent syntax is

arr: Array&lt;[string, number]&gt;

and then change you for loop as well. Something like that.

function names(arr: [string, number][]) {
  let result: string[] = [];
  arr.forEach(([element]) =&gt; {
    result.push(element);
  })
  return result;
}

Note that I use Destructuring assignment to get first element.

答案2

得分: 3

以上提到的解决方案是可以的,但如果你仔细看问题,就可以很容易地使用ES6的map函数编写相同的代码。他们在问题中说它接受一个包含[姓名和年龄]的数组,所以意味着[[姓名1,年龄1],[姓名2,年龄2] .........]

因此,返回类型应该是[string,number][]

这是我的方法:

function names(namesAndAges: [string,number][]): string[] {
  return namesAndAges.map(nameAndAge => nameAndAge[0]);
}

希望能帮到你,它只是使用map函数,并且只从姓名和年龄数组中返回姓名。

英文:

The solution mentioned above is fine but we can easily write the same code with the ES6 map function if you carefully look at the question they are saying it takes an array of [names and ages] so it means [[name1,age1], [name2,age2] .........]

so the return type should be [string, number][]

This is my approach :

function names(namesAndAges: [string,number][]): string[] {
  return namesAndAges.map(nameAndAge =&gt; nameAndAge[0]);
}

Hope it helps it is just using map function and returning only the names from name and age array

huangapple
  • 本文由 发表于 2020年1月4日 01:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定