我尝试加载我的contacts-array并想要循环它们。问题是,我将联系人作为对象而不是数组接收,因此我不能使用.map。这是我的代码片段:
loadConJSON() {
this.http.get('../../assets/data/contacts.json')
.map(res => res.json())
这会导致以下错误:
Property 'map' does not exist on type 'observable<object>'
这是contacts.json的内容:
{
"data": [
{
"_objectInstance": {
"id": 383,
"name": {
"givenName": "",
"honorificSuffix": "",
"formatted": "Schmidt",
"middleName": "",
"familyName": "Schmidt",
"honorificPrefix": ""
}, ...
如何将对象转换为数组?
编辑:我想做这样的事情(构建新的数组):
let contactsArray = contacts.map(contacts =>
({ id: contacts.id,
familyName: contacts.name.familyName,
email: contacts.email.value }));