javascript - Format an object parsed from JSON -
i need create array external json file looks this
{   "cases":[     {          "case_no":1,         "case_input":[         {           "input1":6,           "input2":[1,2,3,4,10,11]         }],         "case_output":31     },     {        "case_no":2,       "case_input":[         {           "input1":5,           "input2":[5,5,5,5,14,17]         }],       "case_output":51     }         ]        } i need create array needs this
["6↵1 2 3 4 10 11","5↵5 5 5 5 14 17"] how can javascript
your input json object not valid has duplicate object keys siblings (input). unless rename them distinct there no way can expected result. assuming, have distinct keys; here jsfiddle
// code goes here 'use strict';  let jsobj = {   "cases":[     {          "case_no":1,         "case_input":[         {           "input":6,           "input2":[1,2,3,4,10,11]         }],         "case_output":31     },     {        "case_no":2,       "case_input":[         {           "input":5,           "input2":[5,5,5,5,14,17]         }],       "case_output":51     }         ]        } let inputarr = []; jsobj['cases'].foreach(function(caseinput) {   caseinput.case_input.foreach(function(caseinput) {     let obj = {};     obj[caseinput.input] = caseinput.input2;     inputarr.push(obj)   }) });  console.log(inputarr); //output
[{6:1,2,3,4,10,11}, {5:5,5,5,5,14,17}]
Comments
Post a Comment