![]() |
Parse Json Object into an two dimensional array in JavaScript - Printable Version +- 0Day Forums (https://0day.red) +-- Forum: Coding (https://0day.red/Forum-Coding) +--- Forum: JScript (https://0day.red/Forum-JScript) +--- Thread: Parse Json Object into an two dimensional array in JavaScript (/Thread-Parse-Json-Object-into-an-two-dimensional-array-in-JavaScript) |
Parse Json Object into an two dimensional array in JavaScript - eugenieoitbgrph - 07-24-2023 I want to parse a JSON Object into an two dimensional array. But my code don't work. Here my example: ``` { "test":[ { "id": 1, "first_name": "Robert", "last_name": "Schwartz", "email": "[email protected]" }, { "id": 2, "first_name": "Lucy", "last_name": "Ballmer", "email": "[email protected]" }, { "id": 3, "first_name": "Anna", "last_name": "Smith", "email": "[email protected]" } ] }; var as = JSON.parse(test); console.log(as) ``` I thought an normalized array I can parse with the parse function. But it don't work. The result array should be like that: ``` [[1,"Robert","Schwartz","[email protected]"],[2,"Lucy","Ballmer","[email protected]"],[3,"Anna","Smith", "[email protected]"]] ``` Thank you for all solutions. RE: Parse Json Object into an two dimensional array in JavaScript - rewarding70 - 07-24-2023 You can simply do this, it will map over tests and return the array contain the values of each object : const twoDimArr= test.map(obj => Object.values(obj)) RE: Parse Json Object into an two dimensional array in JavaScript - malissiammbtffvofu - 07-24-2023 You can't parse a JavaScript object. JSON is a text format. You can only parse text. The array is in the property `text` in your object. You can access it with either `.test` or `['test']`. Then you have to convert each object to an array of values. The simplest way to achieve this is with `map` and `Object.values`. <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> const test = { "test":[ { "id": 1, "first_name": "Robert", "last_name": "Schwartz", "email": "[email protected]" }, { "id": 2, "first_name": "Lucy", "last_name": "Ballmer", "email": "[email protected]" }, { "id": 3, "first_name": "Anna", "last_name": "Smith", "email": "[email protected]" } ] }; const as = test.test.map(el => Object.values(el)); console.log(as) <!-- end snippet --> If you're receiving a string you have to parse it: <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> const test = `{ "test":[ { "id": 1, "first_name": "Robert", "last_name": "Schwartz", "email": "[email protected]" }, { "id": 2, "first_name": "Lucy", "last_name": "Ballmer", "email": "[email protected]" }, { "id": 3, "first_name": "Anna", "last_name": "Smith", "email": "[email protected]" } ] }`; const as = JSON.parse(test).test.map(el => Object.values(el)); console.log(as) <!-- end snippet --> RE: Parse Json Object into an two dimensional array in JavaScript - noralyutjoeough - 07-24-2023 Array of Object Values ====================== Run the array through with [`.map()`][1] method and have each object within the array be converted into an array of values with [`Object.values()`][2]. BTW parsing the array of objects is unnecessary, it's already formatted as an array of plain JavaScript objects correctly. `.map()` and `Object.values()` Methods ---------------------------------- <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> const data = [ { "id": 1, "first": "Robert", "last": "Schwartz", "email": "[email protected]" }, { "id": 2, "first": "Lucy", "last": "Ballmer", "email": "[email protected]" }, { "id": 3, "first": "Anna", "last": "Smith", "email": "[email protected]" } ]; const objToArrVal = arrOfObj => { return arrOfObj.map(obj => Object.values(obj)); } console.log(objToArrVal(data)); <!-- end snippet --> An alternative way to the same result that may be JScript compatible is to: 1. Run the first object in the array of objects (ex. `arrayOfObjects[0]`) in a [`for...in` loop][3] to get an array of object keys (ex. `keyArray = ['id', 'first', 'last', 'email']`) -- (we are assuming that all objects have the same set of keys). 2. Once the array of keys has been made, we run each object of the `arrayOfObjects` in a [`for` loop][4]. 3. On each iteration of the `for` loop we run the object in another `for` loop that will extract each of the object's values (`arrayOfObject[outerLoopCount][keyArray[innerLoopCount]]`) into a sub-array. 4. Once inner and outer loops have completed extracting the object's values into a sub-array (ex. `valueArray`) it is added to the final array of arrays (ex. `resultArray`). `for` Loop and `for...in` Loop ------- <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> const data = [{ "id": 1, "first": "Robert", "last": "Schwartz", "email": "[email protected]" }, { "id": 2, "first": "Lucy", "last": "Ballmer", "email": "[email protected]" }, { "id": 3, "first": "Anna", "last": "Smith", "email": "[email protected]" } ]; function forLoopAO(arrOfObj) { var keyArr = []; var result = []; for (var key in arrOfObj[0]) { keyArr.push(key); } for (var o = 0; o < arrOfObj.length; o++) { var obj = arrOfObj[o]; var valArr = []; for (var k = 0; k < keyArr.length; k++) { valArr.push(obj[keyArr[k]]); } result.push(valArr); } return result; } console.log(forLoopAO(data)); <!-- end snippet --> [1]: [To see links please register here] [2]:[To see links please register here] [3]:[To see links please register here] [4]:[To see links please register here] RE: Parse Json Object into an two dimensional array in JavaScript - camise570 - 07-24-2023 A simple `for loop` should do it let results = []; for (let i=0; i<test.length; i++) { results.push(Object.values(test[i])); } [![enter image description here][1]][1] **EDIT:** If you don't have access to the `Object.values` function you can use another loop to iterate over the object keys and use them to access it's properties let results = []; for (let i=0; i<test.length; i++) { for (let key in test[i]) { results.push(test[i][key]); } } [1]: RE: Parse Json Object into an two dimensional array in JavaScript - secrete184 - 07-24-2023 Implementing @zer00ne answer with ES 6 <!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-js --> const data = [{ "id": 1, "first": "Robert", "last": "Schwartz", "email": "[email protected]" }, { "id": 2, "first": "Lucy", "last": "Ballmer", "email": "[email protected]" }, { "id": 3, "first": "Anna", "last": "Smith", "email": "[email protected]" } ]; const convert = arrOfObj => { const keyArr = Object.keys(arrOfObj[0]); return arrOfObj.map(obj => keyArr.map(key => obj[key])); }; console.log(convert(data)); <!-- end snippet --> |