1

My array:

result = {"ids":[{"tid":"121"}],"tid":[{"tid":"121"}]}

I want to display the value of tid inside ids and tid inside tid using ajax but I don't know how to do it. I have tried result.val(result.ids.tid) but that gives me undefined. I have also tried result.val(result.ids["tid"]) with and without quotes on tid but it still doesn't work.

Does anyone know how I can get the output of 121 from the result array?

17
  • 1
    That isn't an array, that's JSON. Commented Jul 7, 2017 at 18:15
  • You are expected to try to write the code yourself. After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this. Commented Jul 7, 2017 at 18:15
  • 2
    result.ids[0].tid Commented Jul 7, 2017 at 18:16
  • Open up your dev console, if you don't have resutlt available then paste var result = {"ids":[{"tid":"121"}],"tid":[{"tid":"121"}]}. then type result and press enter. You should now be able to traverse into the data and figure out how this works, so you can do it next time as well. Teach a man to fish and all that. Commented Jul 7, 2017 at 18:16
  • @JayBlanchard No, it's not JSON. It's an object. Commented Jul 7, 2017 at 18:18

1 Answer 1

1

You have an object that contains arrays of objects. You should reference the array index to get to the object inside the array.

result.ids[0].tid

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.