0

I am trying to get array key names to new array even if there is another array in array.

So far I am able to get all key names except key names which ones are in one level down.

I have existing array:

searchResult = [
  {"id":1,
   "name":"Duracell",
   "manufacturer":"Duracell",
   "model":"DC2400",
   "type": {
     "id":4,"type":"Nickel Metal Hydride","rechargeable":true
    },
     "size": {
      "size":"AAA","shape":"Cylindrical"
    },
    "nominalCapacity":750,
    "nominalVoltage":1,
    "diameter":10,
    "width":null, 
    "height":44,
    "length":null,
    "depth":null
   },
   {...},
   {...}
]

And I am getting it as props and getting key names:

const formFields = Object.keys(this.props.searchResult[0])
console.log(formFields)

console.log output is:

 ["id",
  "name",
  "manufacturer",
  "model",
  "type",
  "size",
  "nominalCapacity",
  "nominalVoltage",
  "diameter",
  "width",
  "height",
  "length",
  "depth"]

It is missing this:

   "type": {
     "id", "type", "rechargeable"
    },
     "size": {
       "size","shape"
    }

So im expecting it to be something like this:

 ["id",
  "name",
  "manufacturer",
  "model",
  "type": {
    "id", "type", "rechargeable"
   },
   "size": {
     "size","shape"
    },
  "nominalCapacity",
  "nominalVoltage",
  "diameter",
  "width",
  "height",
  "length",
  "depth"]

UPDATE From comment below which contains this link i used code:

 const formFields = this.props.searchResult[0]
 var keys = [];
 for(var key in formFields) {
     keys.push(key);
      if(typeof formFields[key] === "object") {
         var subkeys = getDeepKeys(formFields[key]);
         keys = keys.concat(subkeys.map(function(subkey) {
            return key + "." + subkey;
         }));
      }
  }
 console.log(keys)

console.log output is:

["id", "name", "manufacturer", "model", "type", "size", "nominalCapacity", "nominalVoltage", "diameter", "width", "height", "length", "depth"]

Still not what is expected. Totally same result

6
  • 1
    Possible duplicate of Get all keys of a deep object in Javascript Commented Jan 26, 2019 at 14:33
  • @NikKyriakides Checking it out and will see if it is good for me. Title is a bit different that why could find it, Thanks. Commented Jan 26, 2019 at 14:38
  • You need to use recursion. I'd reckon you can adapt the accepted answer. Commented Jan 26, 2019 at 14:39
  • i keep getting back ["0", "1", "2", "3", "4".... Any idea ? Commented Jan 26, 2019 at 14:54
  • 1
    @NikKyriakides After a while i checked again and yes, it works. Was adapting it in wrong way. Thanks. Commented Feb 4, 2019 at 16:45

1 Answer 1

1

@NikKyriakides comment solved everything. Answer is in this question: Get all keys of a deep object in Javascript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.