I would like to recursively iterate through an object to create one array with all the keys and value pairs in the top level using javascript.
The function would take the following:
obj1 = { name: "Goggles", price: 1499, obj: {no: "hi", yes: "bye"} }
and convert it into this:
{ name: "Goggles", price: 1499, no: "hi", yes: "bye" }
I am using Object.keys(obj1).length
to tell whether or not a property has a value containing multiple properties. The problem is that when it sees a string as a value, it treats it as an Object with each character representing a value:
Object.keys(obj).length
, where obj == name: "Goggles"
retruns 7
this is because Object.keys(...)length interprets it as
{0: G, 1: o, 2: g, 3: g, 4: l, 5: e, 6: s}
Is there a way to differentiate Objects that have multiple key/value pairs from those that don't? Specifically from a key with a value that happens to be a String.