-1

I have a question about this JSON. How to get coordinates from here?

I try to use for(){} like code below but doesn't work.

   item {
        "type": "type1",
        "features": [{
                "type": "typeee1",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        -19.726330999999998,
                        41.360610000000001
                    ]},
                "properties": {
                    "id_strada": "1433",
                    "nome_strada": "test3",
                } },
            {
                "type": "typeee2",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        19.726344999999998,
                        26.36063
                    ] },
                "properties": {
                   id_strada": "13",
                   "nome_strada": "test5",
                } },
            {
                "type": "typeee3",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        19.726358999999999,
                        98.36065
                    ] },
                "properties": {
                  id_strada": "14",
                    "nome_strada": "test34",
                } }, {
                "type": "typeee5",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        19.726372999999999,
                        55.360669999999999
                    ] },
                "properties": {
                    id_strada": "14335",
                    "nome_strada": "test39",
                } }],
        "last_update": "15-08-2019 15:04:45"
    }

function that call JSON is like below.

item: Item[];
        this.ws.getitems().subscribe(
                item => {
                    this.item = item;
                    console.log('this.item.length', this.item.length)
                    for (let i = 0; i < this.item.length; i++) {        
                    }
                }
            );

this.item.length is undefined

My question is, how to get coordinates in here?

Can you ask me any idea please? Thanks!

Alon Bi
  • 83
  • 6
  • Wouldn't you want `item.features.length`? – Heretic Monkey Aug 15 '19 at 13:28
  • 1
    Possible duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Heretic Monkey Aug 15 '19 at 13:28
  • Your javascript object is broken - missing `"` and extra `,` all over the place – Jamiec Aug 15 '19 at 13:30
  • You can test your JSON here: https://jsoneditoronline.org/?id=66ee841768174690807ed68c2c15cf93 – Janneck Lange Aug 15 '19 at 13:34
  • It is also not clear what you want. You say "get coordinates" - but there are lots of coordinates. Do you want an array of arrays? DO you want an object with one of the values as the key and the associated coordinates as the value? – Jamiec Aug 15 '19 at 13:36
  • @JanneckLange https://jsoneditoronline.org/?id=66ee841768174690807ed68c2c15cf93 – Alon Bi Aug 15 '19 at 13:45
  • @HereticMonkey I change `for (let i = 0; i < this.item.features.length; i++) {console.log(this.item.features[i])}` in this console I get `{ "type": "type1", "features": [{ "type": "typeee1","geometry": { "type": "Point","coordinates": [ -19.726330999999998,41.360610000000001]}, "properties": { "id_strada": "1433", "nome_strada": "test3" } }` Now how to get coordinates please? – Alon Bi Aug 15 '19 at 14:43
  • Please, read the linked duplicate. It has an exhaustive answer showing how to figure this out for yourself. Me spoon-feeding you the answer isn't going to help you in your next assignment, or the one after that. Look at the patterns of how your current answers are attempting to answer your question with the incomplete information you've given, and apply it to the actual data you are getting. – Heretic Monkey Aug 15 '19 at 14:48
  • @HereticMonkey Thank you, but I need help, for this I write my code problem. – Alon Bi Aug 15 '19 at 15:02

2 Answers2

0

You don't have an array in an array in an array. You have an array in an object in an object in an array in an object.


interface Feature {
  type: string;
  geometry: {
    type: string;
    coordinates: [ number, number ];
  };
  properties: {
    id_strada: string;
    nome_strada: string;
  };
}

interface Item {
  type: string;
  features: Feature[];
  last_update: string;
}

const items$: Observable<Item> = this.ws.getItems();
const coordinates$: Observable<[number, number]> = items$.pipe(
  switchMap((item: Item) => of(
    ...item.features.map((feature: Feature) => feature.geometry.coordinates)
  )),
);

coordinates$.subscribe((coordinates: [number, number]) => console.log(coordinates));

It is really unclear exactly what your intention is here. Your Item object has multiple coordinates within it. Do you intend to link all of the coordinates, or just the first, or do you want to split them by feature? I've provided you a way to just have an unlinked stream of all coordinates you ever receive. You'll have to figure out what it is you want to do with that.

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • Thank you for answer. I can't get coordinates like you say. How to continue when I subscribe item `this.item = item;` – Alon Bi Aug 15 '19 at 14:04
  • I do not know what your contract of `ws.getItems` is, so I provided you with a snippet that operates directly on the example JSON you provided. Just the method signature of `ws.getItems()` would probably be enough for a more accurate solution for you. – Ian MacDonald Aug 15 '19 at 14:06
  • `.pipe(map((response: HttpResponse) => { return response.body; }` This response.body is item – Alon Bi Aug 15 '19 at 14:09
  • I change for (let i = 0; i < this.item.features.length; i++) {console.log(this.item.features[i])} in this console I get { "type": "type1", "features": [{ "type": "typeee1","geometry": { "type": "Point","coordinates": [ -19.726330999999998,41.360610000000001]}, "properties": { "id_strada": "1433", "nome_strada": "test3" } } Now how to get coordinates please? – Alon Bi Aug 15 '19 at 14:44
0

If you were already in item, coordinates are at item.geometry.coordinates

If your supplied json was x, you could get the first coordinates at x.features[0].geometry.coordinates.

You could find each set of coordinates with:

x.features.forEach(item => {
  let coords = item.geometry.coordinates
  // do something with coords
})
clay
  • 5,917
  • 2
  • 23
  • 21
  • I am in item, I can get coordinates like you say. I have this Model `export class Spots { type : string; geometry:Geometry[]; properties: Properties[]; features: string; coordinates : string; constructor(obj: any) { this.type = obj && obj.type; this.geometry = obj && obj.geometry; this.properties = obj && obj.properties; this.features = obj && obj.features; this.coordinates = obj && obj.coordinates; } }` – Alon Bi Aug 15 '19 at 14:06
  • I change for (let i = 0; i < this.item.features.length; i++) {console.log(this.item.features[i])} in this console I get { "type": "type1", "features": [{ "type": "typeee1","geometry": { "type": "Point","coordinates": [ -19.726330999999998,41.360610000000001]}, "properties": { "id_strada": "1433", "nome_strada": "test3" } } Now how to get coordinates please? – Alon Bi Aug 15 '19 at 14:44