0

I have a mock data object, I created a 2 global JS arrays and assigning them to inside map, and printing, but the object doesn't prints comes as undefined.

Following is what I tried

var mock = {
  "collection_data_status_list": [{
    "input_status_list": [{
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry0"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry1"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry2"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry3"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry4"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }],
    "output_status_list": [{
      "destination_id": "d0",
      "destination_context_id": "t1",
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry0"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }]
  }, {}],
  "query_options": {
    "page_token": "1",
    "page_size": 100
  },
  "result": {
    "request_result": "ACCEPTED",
    "error": {}
  }
};

var input_list = [];
var output_list = [];
var newObj = {};
var new1 = mock.collection_data_status_list;
debugger;
var finalOutput = new1.map(function(item) {
  var input_list = [];
  var output_list = [];

  var newObj = {};
  newObj = {
    input_list: item.input_status_list,
    output_list: item.output_status_list
  };
  return newObj;
});
console.log(newObj);
j08691
  • 204,283
  • 31
  • 260
  • 272
Gaurav
  • 37
  • 5
  • `var newObj = {};` inside your `.map` callback will create a *new* variable that is different to the `newObj` in the outer scope. Since the inner variable shadows the outer one, any references to `newObj` inside the callback refer to the inner variable. So, `console.log(newObj);` prints an object that was never modified – VLAZ Oct 08 '19 at 19:21
  • what are you expecting the output to be? – Neil Oct 08 '19 at 19:22
  • Don't you want `console.log(finalOutput)`? – Barmar Oct 08 '19 at 19:22
  • Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – VLAZ Oct 08 '19 at 19:23
  • What's the purpose of `var input_list` and `var output_list`? You never use those variables. – Barmar Oct 08 '19 at 19:24
  • there is something wrong with the json. but as Barmar stated, you are probably looking for console.log(finalOutput) – Neil Oct 08 '19 at 19:28
  • Yes finalOutput works.. But still was expecting variables input_list & output_list to print values directly.. – Gaurav Oct 08 '19 at 19:40
  • Something like this - var new1 = mock.collection_data_status_list; var input_list = []; var output_list = []; debugger; var finalOutput = new1.map(function(item){ //var newObj = {}; //newObj = { input_list=item.input_status_list; output_list= item.output_status_list; // }; // return newObj; }); console.log(finalOutput); – Gaurav Oct 08 '19 at 19:43

1 Answers1

0
var mock = {
  "collection_data_status_list": [{
    "input_status_list": [{
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry0"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry1"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry2"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry3"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }, {
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry4"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }],
    "output_status_list": [{
      "destination_id": "d0",
      "destination_context_id": "t1",
      "device_id": "r0",
      "sensor_data": {
        "snmp_yang_sensor": {
          "path": "IF-MIB:IF-MIB\/ifTable\/ifEntry0"
        }
      },
      "collection_status": {
        "state": "ACTIVE_STATE",
        "error": {},
        "reported_time": "1569948062"
      }
    }]
  }, {}],
  "query_options": {
    "page_token": "1",
    "page_size": 100
  },
  "result": {
    "request_result": "ACCEPTED",
    "error": {}
  }
};

var new1 = mock.collection_data_status_list;
        var input_list = [];
        var output_list = [];
        debugger;
        var finalOutput = new1.map(function(item){
               input_list=item.input_status_list;
               output_list= item.output_status_list; 
        });

        console.log(finalOutput);
Gaurav
  • 37
  • 5