打字稿从对象中选择编号 [英] Typescript Select Ids from object
本文介绍了打字稿从对象中选择编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Typescript的新手.我想从可观察的位置中选择ID
I am new to Typescript. I want to select ids from observable
这是我的观察
let myObj = [{
"id": 1,
"text": "Mary"
}, {
"id": 2,
"text": "Nancy"
}, {
"id": 3,
"text": "Paul"
}, {
"id": 4,
"text": "Cheryl"
}, {
"id": 5,
"text": "Frances"
}]
预期结果:
let selectedIds = [1,2,3,4,5];
是否可以在不创建数组并将ID推入for循环的情况下执行此操作?
Can I do this without creating an array and pushing the ids in a for loop.
推荐答案
使用Array#map
将一个数组映射到另一个数组:
Use Array#map
to map one array to another:
const myObj = [{"id":1,"text":"Mary"},{"id":2,"text":"Nancy"},{"id":3,"text":"Paul"},{"id":4,"text":"Cheryl"},{"id":5,"text":"Frances"}];
const selectedIds = myObj.map(({ id }) => id);
console.log(selectedIds);
这篇关于打字稿从对象中选择编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文