我有一个JSON数组,并希望将其粘贴到Typescript中的数组变量中。
我从http://www.example.com/select.php:
收到的JSON{
"User":[
{"Name":"Luca M","ID":"1"},
{"Name":"Tim S","ID":"2"},
{"Name":"Lucas W","ID":"3"}
]
}
我想获得这样的数组:
this.items = [
'Luca M',
'Tim S',
'Lucas W'
];
编辑:
当前代码是
this.http.post('http://www.example.com/select.php', creds, {
headers: headers
})
.map(res => res.json().User)
.subscribe(
data => this.data = data.User.map(user => user.Name),
err => this.logError(err),
() => console.log('Completed')
);
this.items = this.data;
错误:
无法读取未定义的属性'地图
我怎么能意识到这一点?
谢谢,最好的问候
对于此非常具体的JSON:
const json = {
"User": [
{ "Name": "Luca M", "ID": "1" },
{ "Name": "Tim S", "ID": "2" },
{ "Name": "Lucas W", "ID": "3" }
]
}
const items = json.User.map(user => user.Name);
console.log(items); // ["Luca M", "Tim S", "Lucas W"]
(操场上的代码(