3

I have one json object as below :

{
  "firstObject": [
    {
      "element": ".branding",
      "details": "Hello"
    },
    {
      "element": "button.login",
      "details": "Hi "
    },
    {
      "element": "#step3",
      "details": "How are you "
    }
  ],
  "secondObject": [
    {
      "element": ".branding",
      "details": "Hello"
    },
    {
      "element": ".step12",
      "details": "Hi "
    },
    {
      "element": "#step2",
      "details": "How are you "
    }
  ]
}

And I have below function where above object is passed as parameter (steps):

function getStateSteps (state, steps) {
    console.log('TESTTT  1', steps);
    let x = Object.keys(steps);
    console.log('TESTTT  2', x);
}

Here, In TESTTT 2 log, 'x' is returning index values of strings instead of key of JSON object.But when I am checking for same object in browser console I am getting the correct output. I have attached the screenshot of my browser console. Can someone please point out what is going wrong?

console screenshot

enter image description here

5
  • Can you check it again? Given your input it seems to work as you expect. Make sure you pass the correct object to your function. Commented Oct 9, 2018 at 5:22
  • 3
    It looks like you didn't parse the JSON you retrieved, ie JSON.parse(steps) or you didn't parse before passing the value to the function. So you are just passing around the JSON text string not an object Commented Oct 9, 2018 at 5:24
  • It is working as expected. what are you trying to achive actually ? Commented Oct 9, 2018 at 5:27
  • 1
    @Narendra, As mentioned by Platinum Industry below I did JSON.parse and it worked. I was trying to get the key of JSON object. Object.keys was working perfectly in console but looks like when I am using inside function I need to parse the object before passing to Object.keys parameter. Thanks! Commented Oct 9, 2018 at 6:44
  • @PatrickEvans..Yeah..I added JSON.parse and its working. Thanks! Commented Oct 9, 2018 at 6:45

4 Answers 4

2

Check it again. I have just tried it in my console and it prints exactly as expected.

enter image description here

Try JSON.parse(steps) before using the json object and see if that helps.

function getStateSteps (state, steps) {
    steps = JSON.parse(steps)
    console.log('TESTTT  1', steps);
    let x = Object.keys(steps);
    console.log('TESTTT  2', x);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It worked. but any idea why and how it was returning the array of index?
@dev it will still print the array as a string in chrome console if you log it. But its just a string, not a json object, thats why you couldnt manipulate it in your code
0

This works fine:

const data = {
  "firstObject": [
    {
      "element": ".branding",
      "details": "Hello"
    },
    {
      "element": "button.login",
      "details": "Hi "
    },
    {
      "element": "#step3",
      "details": "How are you "
    }
  ],
  "secondObject": [
    {
      "element": ".branding",
      "details": "Hello"
    },
    {
      "element": ".step12",
      "details": "Hi "
    },
    {
      "element": "#step2",
      "details": "How are you "
    }
  ]
};

function getStateSteps (state, steps) {
    console.log('TESTTT  1', steps);
    let x = Object.keys(steps);
    console.log('TESTTT  2', x);
}

getStateSteps(null, data);

Have you stripped this from a broader implementation and perhaps missed a key function?

Whats your dev environment?

2 Comments

Yeah..As mentioned by Platinum Industry I was missing JSON.parse .Thanks!
Glad that was it!
0

This can happen when the key was not put inside quotation "" One more example is if you are using for (a in Object.keys(ob)) then a contains the keys of the subobjects of ob so the correct way is for(a in ob)

Comments

0

Let me explain why a JSON array would return numeric index values instead of the KEY when using Object.keys.

If your JSON is an array of objects like so...

const thisExample = [ {keys:values}, {keys:values}, {keys:values} ]

then notice that 'thisExample' is just an array[]. So each object{} in that array is at an index within that array, starting at 0 and counting up from there. Technically index values in an array are Keys also. So Object.key is returning that index, or key, value of that array.

Object.keys isn't smart. It gets the index value, which again is also a key. You need to tell it to go deeper and get the keys within the object {}.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.