![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create array from JSON object JavaScript
Suppose, we have the following JSON object −
const obj = { "test1": [{ "1": { "rssi": -25, } }, { "2": { "rssi": -25, } }], "test2": [{ "15": { "rssi": -10, } }, { "19": { "rssi": -21, } }] };
We are required to write a JavaScript function that takes in an object like this −
The function should then map the "rssi" property of all the nested objects to a corresponding nested array of arrays.
Therefore, for the above array, the output should look like this −
const output = [[-25, -25], [-10, -21]];
Example
const obj = { "test1": [{ "1": { "rssi": -25, } }, { "2": { "rssi": -25, } }], "test2": [ { "15": { "rssi": -10, } }, { "19": { "rssi": -21, } }] }; const mapToValues = (object = {}) => { const res = []; for (let key in object) { let obj = object[key]; let aux = []; for (let i = 0; i < obj.length; i++) { for (x in obj[i]) { aux.push(obj[i][x].rssi); } } res.push(aux); } return res; }; console.log(mapToValues(obj));
Output
And the output in the console will be −
[ [ -25, -25 ], [ -10, -21 ] ]
Advertisements