-2

How read dynamic keys in JSON , NodeJs and or JQuery and or JavaScript?

I did not find. I did try.

Need find out name keys, and read values.

Keys dynamic

{
  "marka1": {
    "name": "Mika",
  },
  "beti1": {
    "name": "Yii",
  }
}
Hacketo
  • 4,978
  • 4
  • 19
  • 34

2 Answers2

5

Need find out name keys, and read values.

var obj = {
  "marka1": {
    "name": "Mika",
  },
  "beti1": {
    "name": "Yii",
  }
};

you can get the keys inside this object by

var keys = Object.keys( obj);
console.log(keys);

now you can iterate this keys array to get values of each property.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

The "key" should be a property of the object and not the name of the object. The data structure should look more like :

[ { key : "marka1", name : "Mika" } ]

With it being an array of objects, this will allow you to reference the key of each object.

If the data is formatted like that, use Object.keys() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys to grab the key

erichardson30
  • 4,984
  • 8
  • 26
  • 47