I have two level nested object. I need to convert it to array.
var data = {
"6-7":
{
"0": "06:00",
"1": "06:10",
"2": "06:20",
"3": "06:30",
"4": "06:40",
"5": "06:50",
"6": null,
"7": null,
"8": null,
"9": null,
"10": null,
"11": null,
"12": null,
"13": null,
"14": null
},
"7-8":
{
"0": "07:00",
"1": "07:04",
"2": "07:08",
"3": "07:12",
"4": "07:16",
"5": "07:20",
"6": "07:24",
"7": "07:28",
"8": "07:32",
"9": "07:36",
"10": "07:40",
"11": "07:44",
"12": "07:48",
"13": "07:52",
"14": "07:56"
}
}
I tried Object.values(obj).map(elem => elem)
it converted to array but still i can't convert it right.
I need to convert it like [["06:00","06:10",...],[07:00,07:04,...]]
Thanks for any kind of help.
Edit : data is always same shape not going deeper
CodePudding user response:
var data = {
"6-7":
{
"0": "06:00",
"1": "06:10",
"2": "06:20",
"3": "06:30",
"4": "06:40",
"5": "06:50",
"6": null,
"7": null,
"8": null,
"9": null,
"10": null,
"11": null,
"12": null,
"13": null,
"14": null
},
"7-8":
{
"0": "07:00",
"1": "07:04",
"2": "07:08",
"3": "07:12",
"4": "07:16",
"5": "07:20",
"6": "07:24",
"7": "07:28",
"8": "07:32",
"9": "07:36",
"10": "07:40",
"11": "07:44",
"12": "07:48",
"13": "07:52",
"14": "07:56"
}
}
const array = Object.values(data).map(Object.values)
console.log(array);
CodePudding user response:
Below is one possible way to achieve the target.
Code Snippet
// recursive method to convert object to array
const myConv = obj => (
Object.entries(obj).flatMap( // iterate over key-value pairs
([k, v]) => (
v
? typeof v === 'object' // if nested value is object
? [myConv(v)] // nested-array & make the recursive call
: v // else, simply return the value as-is
: null // if value is null, return null
)
).filter(Boolean) // filter to remove falsy values
);
const data = {
"6-7": {
"0": "06:00",
"1": "06:10",
"2": "06:20",
"3": "06:30",
"4": "06:40",
"5": "06:50",
"6": null,
"7": null,
"8": null,
"9": null,
"10": null,
"11": null,
"12": null,
"13": null,
"14": null
},
"7-8": {
"0": "07:00",
"1": "07:04",
"2": "07:08",
"3": "07:12",
"4": "07:16",
"5": "07:20",
"6": "07:24",
"7": "07:28",
"8": "07:32",
"9": "07:36",
"10": "07:40",
"11": "07:44",
"12": "07:48",
"13": "07:52",
"14": "07:56"
}
};
console.log(myConv(data));
.as-console-wrapper { max-height: 100% !important; top: 0 }
Explanation
Inline comments added in the snippet above.
CodePudding user response:
You want just the values of the values, so its as simple as:
const result = Object.values(data).map( v => Object.values(v) );
If you need to filter out the null
s then its a tiny bit extra
const result = Object.values(data).map( v => Object.values(v).filter(x => x != null) );
Live example of the latter
var data={"6-7":{0:"06:00",1:"06:10",2:"06:20",3:"06:30",4:"06:40",5:"06:50",6:null,7:null,8:null,9:null,10:null,11:null,12:null,13:null,14:null},"7-8":{0:"07:00",1:"07:04",2:"07:08",3:"07:12",4:"07:16",5:"07:20",6:"07:24",7:"07:28",8:"07:32",9:"07:36",10:"07:40",11:"07:44",12:"07:48",13:"07:52",14:"07:56"}};
const result = Object.values(data).map( v => Object.values(v).filter(x => x != null) );
console.log(result)