N
N
nutritsio2017-10-26 22:50:02
JavaScript
nutritsio, 2017-10-26 22:50:02

How to add one more value to an array of objects, to each object?

I have an array of object:

[
  {
    "id": "1",
    "title": "Дом1"
  },
  {
    "id": "2",
    "title": "Зал2"
  },
  {
    "id": "3",
    "title": "Дом3"
  },
  {
    "id": "4",
    "title": "Зал4"
  }
]

Need an array of object:
[
  {
    "id": "1",
    "title": "Дом1",
    "active": "true"
  },
  {
    "id": "2",
    "title": "Зал2",
    "active": "false"
  },
  {
    "id": "3",
    "title": "Дом3",
    "active": "false"
  },
  {
    "id": "4",
    "title": "Зал4",
    "active": "false"
  }
]

When trying on typescript, Angular 4 throws this error:
59f23a4c96991861638749.png
Error:
ERROR TypeError: Cannot set property 'active' of undefined
59f23b01b8d46971291937.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman, 2017-10-26
@r0n1x

map
forEach?

var a = [{
      "id": "1",
      "title": "Дом1"
    }, {
      "id": "2",
      "title": "Зал2"
    }, {
      "id": "3",
      "title": "Дом3"
    }, {
      "id": "4",
      "title": "Зал4"
    }];
    for (let i of a) {
      i.active = false;
    }
    console.log(a);

M
mrspd, 2017-10-27
@mrspd

arr.forEach((obj) => obj['active'] = false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question