admin管理员组

文章数量:1356716

Hello I am writing an angular app. I have a Node API returning data which I am trying to chart using Chart.js. I am getting my data from the API and it returns an object like this:

{
  "0": {
    "_id": "62716061f6897b637f59ba9e",
    "created": "2022-05-03T17:03:29.459Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.42499542,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62716061f6897b637f59ba9f"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.75,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62716061f6897b637f59baa0"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62716061f6897b637f59baa1"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa2"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa3"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa4"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa5"
      }
    ],
    "__v": 0
  },
  "1": {
    "_id": "62715f33f6897b637f59ba92",
    "created": "2022-05-03T16:58:27.064Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.3125,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba93"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba94"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba95"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba96"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba97"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba98"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba99"
      }
    ],
    "__v": 0
  },
  "2": {
    "_id": "62715e04f6897b637f59ba72",
    "created": "2022-05-03T16:53:24.706Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.19999695,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba73"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba74"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba75"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba76"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba77"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba78"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba79"
      }
    ],
    "__v": 0
  },
  "3": {
    "_id": "62715cd6f6897b637f59ba66",
    "created": "2022-05-03T16:48:22.086Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.19999695,
        "metric": 19,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba67"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba68"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba69"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6a"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6b"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6c"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6d"
      }
    ],
    "__v": 0
  }
}

I am trying to iterate over this to parse my data points but I am getting errors. Here is my code:

  constructDataSet(input: IGreenhouseData[]) {
    let labels: string[] = [];
    let imperial: number[] = [];
    let metric: number[] = [];
    let datasets: IDataSet[] = [];

    for (let item of input) {
      for (let dataPoint of item.items) {
        labels.push(dataPoint.name);
        imperial.push(dataPoint.imperial);
        metric.push(dataPoint.metric);
      }
      let newDataSet: IDataSet = {
        label: 'Imperial',
        data: imperial,
      };
      datasets.push(newDataSet);

      newDataSet = {
        label: 'Metric',
        data: metric,
      };
      datasets.push(newDataSet);
    }

    this.data = {
      labels: labels,
      datasets: datasets,
    };
  }

This is the error: ERROR TypeError: input is not iterable

Hello I am writing an angular app. I have a Node API returning data which I am trying to chart using Chart.js. I am getting my data from the API and it returns an object like this:

{
  "0": {
    "_id": "62716061f6897b637f59ba9e",
    "created": "2022-05-03T17:03:29.459Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.42499542,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62716061f6897b637f59ba9f"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.75,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62716061f6897b637f59baa0"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62716061f6897b637f59baa1"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa2"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa3"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa4"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62716061f6897b637f59baa5"
      }
    ],
    "__v": 0
  },
  "1": {
    "_id": "62715f33f6897b637f59ba92",
    "created": "2022-05-03T16:58:27.064Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.3125,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba93"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba94"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715f33f6897b637f59ba95"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba96"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba97"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba98"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715f33f6897b637f59ba99"
      }
    ],
    "__v": 0
  },
  "2": {
    "_id": "62715e04f6897b637f59ba72",
    "created": "2022-05-03T16:53:24.706Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.19999695,
        "metric": 19.0625,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba73"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba74"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715e04f6897b637f59ba75"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba76"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba77"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba78"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715e04f6897b637f59ba79"
      }
    ],
    "__v": 0
  },
  "3": {
    "_id": "62715cd6f6897b637f59ba66",
    "created": "2022-05-03T16:48:22.086Z",
    "items": [
      {
        "name": "Air Temperature",
        "imperial": 66.19999695,
        "metric": 19,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba67"
      },
      {
        "name": "Outside Air Temperature",
        "imperial": 65.63749695,
        "metric": 18.6875,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba68"
      },
      {
        "name": "Water Temperature",
        "imperial": 65.75,
        "metric": 18.75,
        "controlStatus": false,
        "_id": "62715cd6f6897b637f59ba69"
      }
    ],
    "settings": [
      {
        "name": "in1",
        "onValue": 80,
        "offValue": 78,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6a"
      },
      {
        "name": "in2",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6b"
      },
      {
        "name": "in3",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6c"
      },
      {
        "name": "in4",
        "onValue": 0,
        "offValue": 0,
        "status": "",
        "unitType": "Temp",
        "_id": "62715cd6f6897b637f59ba6d"
      }
    ],
    "__v": 0
  }
}

I am trying to iterate over this to parse my data points but I am getting errors. Here is my code:

  constructDataSet(input: IGreenhouseData[]) {
    let labels: string[] = [];
    let imperial: number[] = [];
    let metric: number[] = [];
    let datasets: IDataSet[] = [];

    for (let item of input) {
      for (let dataPoint of item.items) {
        labels.push(dataPoint.name);
        imperial.push(dataPoint.imperial);
        metric.push(dataPoint.metric);
      }
      let newDataSet: IDataSet = {
        label: 'Imperial',
        data: imperial,
      };
      datasets.push(newDataSet);

      newDataSet = {
        label: 'Metric',
        data: metric,
      };
      datasets.push(newDataSet);
    }

    this.data = {
      labels: labels,
      datasets: datasets,
    };
  }

This is the error: ERROR TypeError: input is not iterable

Share Improve this question edited May 3, 2022 at 18:11 R. Richards 25.2k10 gold badges66 silver badges65 bronze badges asked May 3, 2022 at 17:04 jak devjak dev 451 gold badge2 silver badges8 bronze badges 6
  • 1. Paste the input as valid json in the question 2. Which line gives you the error? – Nicholas K Commented May 3, 2022 at 17:06
  • 1 input is not an array – Nonik Commented May 3, 2022 at 17:10
  • @NicholasK Line7 (the for loop) gives the error. – jak dev Commented May 3, 2022 at 17:11
  • @Nonik 'input' is the variable name with this JSON data in it. That is not an array? – jak dev Commented May 3, 2022 at 17:12
  • input is an Object not an array. – Nicholas K Commented May 3, 2022 at 17:13
 |  Show 1 more ment

1 Answer 1

Reset to default 6

input is not an array, but an object. Change the for-loop to:

for (let item of Object.values(input)) {  
   
   /*rest of the code*/ 

}

so the iteration is done over the object values rather than the object itself.

本文标签: javascriptWhy is this not iterableStack Overflow