As a newcomer to typescript, I am looking for a way to extract values from JSON objects within an array that have changing property names.
The code I have (simplified) is as follows:
const gridData = [
{ "source_language": "en", "word": "man", "character_grid": [["i", "q", "\u00ed", "l", "n", "n", "m", "\u00f3"], ["f", "t", "v", "\u00f1", "b", "m", "h", "a"], ... ], "target_language": "es" },
...
];
const gridDataInUse = gridData[0];
const selectedLetters = [{x: 6, y: 1, l: "h"}, {x: 6, y: 2, l: "o"}, ...]
const letterCoordinates: number[] = []
selectedLetters.forEach((letter => {
letterCoordinates.push(letter.x);
letterCoordinates.push(letter.y);
}));
const letterCoordinatesAsString = letterCoordinates.join(',');
const selectedWord = selectedLetters.map((letter) => {
return letter.l;
}).join('');
console.log(gridDataInUse.word_locations[letterCoordinatesAsString]);
In this example, my intention is to retrieve the value "hombre" from the first JSON object in the array.
However, I encountered the following error message:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ "6,1,6,2,6,3,6,4,6,5,6,6": string; ... No index signature with a parameter of type 'string' was found on type '{ "6,1,6,2,6,3,6,4,6,5,6,6": string; ...
Can you guide me on how to resolve this error?
Thank you very much for your assistance!