I have an object that look like this:
{
"id": 45745049
"seller": {
"first_name": "Sam",
"last_name": "Smith",
"email": "samsmith@smith.com",
"phone": {
"number": "1111-1111",
"verified": false
},
},
"order_items": [
{
"item": {
"id": "29239765",
"title": "item1",
"colors": [
"red",
"green",
"blue"
]
},
"quantity": 1,
"unit_price": 230,
},
{
"item": {
"id": "238457363",
"title": "item2",
"colors": [
"red"
]
},
"quantity": 2,
"unit_price": 110,
}
],
"date_created": "2020-08-03T12:17:25.000-04:00",
"date_last_updated": "2020-08-03T16:51:35.61Z"
}
I want an array with pairs of EVERY key in the object with the value.
For example:
[
["id", 45745049],
["first_name", "Sam"],
.....,
["phone.number", "1111-1111"],
["phone.verified", false],
....etc
]
Everything Ok until that point. The problem is when a property is an array of objects. The output I want is the following:
[
...,
["order_items1.item.id", 29239765],
["order_items1.item.colors1", "red"],
["order_items1.item.colors2", "green"],
...,
["order_items2.item.id", 238457363],
["order_items2.item.colors1", "red"],
...etc
]
So it needs to check if the property is an array and add the position number if so.
I know I need a recursive function but I dont know how to do it. This is what I got until now.
getObjectKeys = (obj) => {
let FieldName = "";
let FieldValue = "";
for(var prop in obj) {
FieldName += prop;
if(!(prop instanceof Array) && (typeof prop !== "object") && obj[prop]) {
FieldValue = obj[prop];
} else if(prop instanceof Array && prop.length !== 0){
prop.forEach((innerItem, i) => {
FieldName += `${i+1}.`;
// check if the inner item is an array or whatever an do it all again
// Dont know what to do here.
});
} else {
getObjectKeys(obj[prop]);
}
}
return [FieldName, FieldValue];
}
Note: I dont want the empty or null keys.
I would be very grateful if someone can help me. Thanks anyways!