Touring Angular object 2 paging information

0

I'm working with Angular 2.

First of all I do an API query and retrieve the information in JSON.

With this method:

getinvfechasensores(fecha1,fecha2){

    return this._http.get(this.url+'getinvfechasensores/' +fecha1 + '/' + fecha2)
             .map(res => res.json());

  }

Then I subscribe to this method with my component

mostrarnombre(){
  this._invService.getinvfechasensores(this.fecha1,this.fecha2).subscribe(
      response => {
      if (!response) {
        console.log('error al cargar datos');
      } else {
        this.invernadero = response;
        console.log(this.invernadero);
        var f0=this.invernadero.map(item => item.recvTime);
      }

the greenhouse variable is my object and what I do is assign the JSON response to that object:

export class Invernadero{
  constructor(
    public recvTimeTs: string,
    public recvTime: string,
    public fiwareServicePath: string,
    public entityId: string,
    public entityType: string,
    public attrName: string,
    public attrType: string,
    public attrValue: string,
    public attrMd: string,
    public invjuancol: number
  ){}

}

The structure of my JSON is this:

[
    {
        "attrMd": null,
        "attrName": "sensor1",
        "attrType": "Integer",
        "attrValue": "22",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 141,
        "recvTime": "2017-09-28T18:09:31.000Z",
        "recvTimeTs": null
    },
    {
        "attrMd": null,
        "attrName": "sensor2",
        "attrType": "Integer",
        "attrValue": "26",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 142,
        "recvTime": "2017-09-28T18:09:39.000Z",
        "recvTimeTs": null
    },
    {
        "attrMd": null,
        "attrName": "humedad",
        "attrType": "Integer",
        "attrValue": "53",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 143,
        "recvTime": "2017-09-28T18:09:41.000Z",
        "recvTimeTs": null
    }
]

My question is this:

How can I go through my greenhouse object and look in the field attrName for the name sensor1 and group it in a new string called sensor1 and look for sensor2 and group it in a string2?

But in turn everything that has attrName = sensor1 has to be in String1, everything attrName = sensor2 save it in String2

I'm going through the object in this way, I do not know if it's the right one.

for (let i = this.invernadero.length - 1; i >= 0; i--) {
              console.log('LO QUE CONTIENE EL FOR' + i); // "4", "5", "6"
            }

Greetings and thanks.

    
asked by Manolait 10.10.2017 в 15:42
source

1 answer

0

You have a Greenhouse class, but you do not really use it as such because you do not use its constructor. When the response is passed from JSON to a Javascript object, you are creating objects with the same attributes, but they are not really Greenhouse class. What you can do is declare Greenhouse as an interface.

If you want to stay with only attrValue if there is an element with attrName=="sensor1" you can do something like the following:

var invernaderos=[
    {
        "attrMd": null,
        "attrName": "sensor1",
        "attrType": "Integer",
        "attrValue": "22",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 141,
        "recvTime": "2017-09-28T18:09:31.000Z",
        "recvTimeTs": null
    },
    {
        "attrMd": null,
        "attrName": "sensor2",
        "attrType": "Integer",
        "attrValue": "26",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 142,
        "recvTime": "2017-09-28T18:09:39.000Z",
        "recvTimeTs": null
    },
    {
        "attrMd": null,
        "attrName": "humedad",
        "attrType": "Integer",
        "attrValue": "53",
        "entityId": "palmerillas",
        "entityType": "invernadero",
        "fiwareServicePath": "ualiof",
        "invjuancol": 143,
        "recvTime": "2017-09-28T18:09:41.000Z",
        "recvTimeTs": null
    }
]

if (invernaderos.some(inv => inv.attrName=='sensor1')) {
  let valores= invernaderos.map(inv => inv.attrValue);
  console.log(valores);
}
    
answered by 10.10.2017 / 17:09
source