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