Creating json object in js file

Hello everyone,

I'm currently trying to parse some data I receive from a request to send only what I want to the html file. I manage to get the data like I want, but when I try to create my object, I can't parse it properly. Here's what I currently have as JS script:

var itemList = [];
items.thermostatList.forEach(function (value,index){
	if ( != ""){
		itemList += {
			"value": "Thermostat: " +
	value.remoteSensors.forEach( function (val, ind){
console.log("itemList: " + itemList);
var keyInfo = Object.keys(itemList);
console.log("itemList info: " + keyInfo);

Here's the console output:

itemList: [object Object]
itemList info: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14

Now, problem is the keys are number. How do I access that?

I tried itemList.forEach(....) and that failed saying forEach is not a function of itemList.
I tried itemList[0] and itemList.0, both failed to compiled. The goal here is to finish with:

var keys = { 
	items : itemList

So afterward, on my html file, I can parse the json and get all items.

Thank you

An array is an object and when you do this...

        itemList += {
			"value": "Thermostat: " +

I dont think it is having the affect you want (or maybe incidentally it is!)

Any how, to add to an array, you use push


    "value": "Thermostat: " +

Then you can access items as an array itemList[0] and itemList[0].value etc.

Hello, thanks a lot for the information, it worked perfectly!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.