I have a function that breaks a string into an array of keyword substrings where "hello" gives
keywords
0: "h"
1: "he"
2: "hel"
3: "hell"
5: "hello"
I need to refactor into a json object:
keywords
"h": true
"he": true
"hel": true
"hell": true
"hello": true
The working array function is
function createKeywords (name: string) {
const keywords: string[] = [];
let keyword = "";
name.split("").forEach((letter) => {
keyword += letter;
keywords.push(keyword);
});
return keywords;
};
and my broken refactor attempt is
function createKeywords (name: string) {
const keywords: {text: string, value: boolean}[] = [];
let keyword = "";
name.split("").forEach((letter) => {
keyword += letter;
keywords[keyword] = true; // Element implicitly has an 'any' type because index expression is not of type 'number'.
});
return keywords;
};
Which gives the above error?