打字稿从对象数组中筛选键值

时间:2020-08-13 10:50:21

标签: typescript

我有以下对象数组

the size of callocked array is 8

我需要一个具有'position'键值的结果数组

var a: Array = [{ position: 3, name: "Lithium", weight: 6.941, … },{ position: 5, name: "Boron", weight: 10.811, … }{ position: 6, name: "Carbon", weight: 12.0107, … }]

我需要一个打字稿中的方法来获得这个结果数组。

1 个答案:

答案 0 :(得分:2)

var resultArray = a.map(e => e.position)

根据Mathyn的建议,我在回答中说明了该解决方案的工作方式和原因:

Array.map()为数组的每个元素执行一个回调函数。此map-call的结果是回调调用的结果的数组。

MDN web docs: Array.prototype.map()

相关问题