Typescript Select 来自对象的 ID
Typescript Select Ids from object
我是 Typescript 的新手。我想 select 来自 observable
的 ID
这是我的观察结果
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 循环的情况下执行此操作吗?
使用Array#map
将一个数组映射到另一个数组:
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);
<script>
..........Your Ajax / JSON Request Sending Code
function success(response)
{
var arr = JSON.parse(response);
var selectedIds = new Array();
for(var i=0;i < arr.length ; i++)
{
selectedIds["[" + arr[i].id + "]"] ;
}
document.write(selectedIds);
}
</script>
我是 Typescript 的新手。我想 select 来自 observable
的 ID这是我的观察结果
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 循环的情况下执行此操作吗?
使用Array#map
将一个数组映射到另一个数组:
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);
<script>
..........Your Ajax / JSON Request Sending Code
function success(response)
{
var arr = JSON.parse(response);
var selectedIds = new Array();
for(var i=0;i < arr.length ; i++)
{
selectedIds["[" + arr[i].id + "]"] ;
}
document.write(selectedIds);
}
</script>