7

I have an array of object.structure is like that.

animal = [{"cow":{"leg":4,"eye":2}},{"monkey":{"leg":2,"eye":2}}]

here first key is dynamic like cow and monkey

so my question is how can i access the key leg if first key is dynamic

3
  • 1
    How would you determine if you want "cow" or "monkey" or anything else? Commented Jul 24, 2019 at 14:02
  • 3
    Loop over the keys using Object.keys() and, for each one, check if the key's value contains leg. Or, if it will only ever be one key, no need to loop; you can just do Object.keys(obj)[0] to get the first key. Or, better yet, if you don't actually need the key name and just want to know if leg exists, use Object.values(obj)[0].leg. Commented Jul 24, 2019 at 14:02
  • What do you mean by dynamic.U need to loop through the array and use object.keys function to get the key name and use that to get value inside the array .If you are uncertain of these (monkey,cow...) keys Commented Jul 24, 2019 at 14:02

6 Answers 6

11

If you are sure that each object within the array has only 1 property (which would be the type of animal), you can do something like this.

animals = [{"cow":{"leg":4,"eye":2}},{"monkey":{"leg":2,"eye":2}}];
    
for (let animal of animals) {
  let propName = Object.keys(animal)[0];
  let result = animal[propName];
  console.log(result); // <- Do what you want with it
}

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

3 Comments

@TylerRoper Thank you, I made it as an inline snippet as you suggested. Also, with the array supplied it works, however I will try to come up with a better solution.
@vivek_23 I'm not iterating over the array with Object.keys(), I'm only using it to get the keys of each animal.
@DavidFontes Ahh!! Overlooked that.
3

You can use the newly added Object.entries, which make the most sense imo.

Thus for your data you have:

const animal = ...
for (a of animal) {
    const [animalName, animalDesc] = Object.entries(a)[0]; // assumes there is only one
}

Comments

1

This will give you an array of objects with the contents of each animal.

animals.map(animal => animal[Object.keys(animal)[0]]);

Comments

1

1) Find an animal with that key

2) Return it with [animal]

3) Use it as you would (.leg)

const animals = [{ cow: { leg: 4, eye: 2 } }, { monkey: { leg: 2, eye: 2 } }];

const animal = 'cow';
const leg = animals.find(a => !!a[animal])[animal].leg;

Comments

0

Maybe convert it into a "non-dynamic-key" object?

const animal = [{"cow":{"leg":4,"eye":2}},{"monkey":{"leg":2,"eye":2}}]
const newanimal = animal.map(obj => {
  const keys = Object.keys(obj)
  return keys.map(key => ({ key, value: obj[key] }))
}).flat(Math.Infinity)

console.log(newanimal)

2 Comments

@TylerRoper thanks for the hint! or I could flatten the array using flat
You can, although you're using an additional iteration (map -> flat) as opposed to a single flatMap. Not sure what the efficiency difference would be though so it may be negligible.
0

You can use map & Object.keys. map will return a new array. Object.keys is use to get all the keys inside each of the object in animal array.Then this line elem[getKeys].leg will retrieve the value of the leg key

let animal = [{
  "cow": {
    "leg": 4,
    "eye": 2
  }
}, {
  "monkey": {
    "leg": 2,
    "eye": 2
  }
}];

let legVal = animal.map((elem) => {
  let getKeys = Object.keys(elem)[0];
  return elem[getKeys].leg
})

console.log(legVal)

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.