0

In Javascript you can iterate through a JSON array by using something like moves[i].name in a loop where moves is the JSON array, i is the index, and .name is the value. In typescript, however, when I try to iterate through a JSON array, the same syntax fails.

Typing `let name:String = totalMap[i].title; throws the error Element implicitly has an 'any' type because type 'JSON' has no index signature.

Is this a difference with Typescript or my code in general?

totalMap is initialized as (<any>map).allAreas where map is a JSON object.

The array is

"allAreas" : [{
    "area1": {
        "title": "River",
        "description": "A quiet river deep inside a forest",
        "hazard": "All clear",
        "start": true,
        "north" : "Open field",
        "south" : "none",
        "east" : "none",
        "west" : "none"
    },..]

and then it continues on with more areas

5
  • post your array in the qeustion Commented Jan 27, 2018 at 4:37
  • Where are you getting the JSON from? What creates map? Commented Jan 27, 2018 at 4:45
  • Map is created by loading in a JSON file using require, like map = require('./files.json'); Commented Jan 27, 2018 at 4:55
  • read this: stackoverflow.com/questions/42400298/… Commented Jan 27, 2018 at 5:25
  • I can't reproduce this issue. Please post more of your code. Do you have some ambient type definition for JSON? Commented Jan 27, 2018 at 6:08

0

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.