Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 803 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse Json Object into an two dimensional array in JavaScript

#1
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.
Reply

#2
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))
Reply

#3
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 -->
Reply

#4
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]

Reply

#5
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]:
Reply

#6
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 -->

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through