我是Typescript的新手。我想从可观察中选择ID 我有一个数组如下。请帮助我获得预期的输出。
const Input=[{
"id": 1,
"text": "My Choice 1"
}, {
"id": 2,
"text": "My Choice 2"
}, {
"id": 3,
"text": "My Choice 3"
}, {
"id": 4,
"text": "My Choice 4"
}, {
"id": 5,
"text": "My Choice 5"
}];
预期结果:
let selectedIds = [
{id: "Choice", name: "2"},
{id: "Choice", name: "3"},
{id: "Choice", name: "5"}];
答案 0 :(得分:1)
使用array.map转换对象
const Input=[{
"id": 1,
"text": "My Choice 1"
}, {
"id": 2,
"text": "My Choice 2"
}, {
"id": 3,
"text": "My Choice 3"
}, {
"id": 4,
"text": "My Choice 4"
}, {
"id": 5,
"text": "My Choice 5"
}];
let Result = Input.map(choice => ({ id: "choice", name: choice.id }));
console.log(Result);
答案 1 :(得分:0)
let selectedIds = Input.map(item =>
{
return {
id: item.text,
name: item.id
};
})