'Why is Array.isArray always returning false here?
I'm trying to cast form data to a custom type, but I'm having trouble getting it to recognize an array in the custom type. I saw this method begin used in another similar problem, but it isn't working for me. Is there another way of doing this?
I need to detect the array type as using value as string splits the string character by character instead of putting the string to the first index. I can't use .push() either, because it doesn't know if object[key] is an array or not.
Simplified version of the custom type:
export type CustomType = {
id: number;
name: string;
items: string[];
[key: string]: (string | string[] | number)
}
Here is the code:
const form = document.getElementById("form") as HTMLFormElement;
const data = new FormData(form);
const object = {} as CustomType;
data.forEach((value, key)=> {
if(Array.isArray(object[key])) { // <---- Always false
const arr = value.toString().split(",");
object[key] = arr;
}
else {
object[key] = value as string;
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
