A
A
Alexander Belov2017-02-03 23:24:03
Angular
Alexander Belov, 2017-02-03 23:24:03

How to write an array of objects from JSON / Angular2 to a variable?

In an Angular2 service, I access an array of objects stored in a separate json file.

export class CartService {
  constructor(private http: Http){ }
  
  getCartItems() {
    return this.http.get('../cart.json')
    .map(
      (res) => res.json()
    );
  }
}

JSON:
[
        {
          "id":"id-1",
          "title": "Title-1",
          "image": "https://bla.bla/bla/01.jpg",
          "slider": "https://bla.bla/bla//slider01.jpg",
          "description": "Description-1",
          "price": 67887,
          "quantity": " "
        },

        {
          "id":"id-2",
          "title": "Title-2",
          "image": "https://bla.bla/bla/02.jpg",
          "slider": "https://bla.bla/bla//slider02.jpg",
          "description": "Description-2",
          "price": 69007,
          "quantity": " "
        },
...,
...,
...
]

How to transfer the contents of an array to some variable, so that later you can work with it in the application - delete, iterate, add objects and values, etc.?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Sharonov, 2017-02-04
@k_sharonov

As an option, make a storage service and store this array there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question