我有一个需要转换的对象。我正在尝试将对象转换为FormData
以上传文件
对象:
obj{
a:
{
a1: 'test1',
a2: 'test2'
}
b:
{
b1: 'test3',
c1: 'test4',
}
}
转换为:
{
obj[a][a1]: test1,
obj[a][a2]: test2,
obj[b]: binarydata // I want to convet this to binary data
}
我现在无法使用的内容:
const formData = new FormData()
Object.keys(object).forEach(key => formData.append(key, object[key]));
我不是要展平数组。我正在尝试将其转换为
之类的对象obj[key1][key2][..]: value
答案 0 :(得分:0)
尝试使用Object.entries()
。例如...
// If this is the object you want to convert to FormData...
const item = {
description: 'First item',
price: 13,
photo: File
};
const formData = new FormData();
Object.entries(item).forEach(([key, value]) => {
formData.append(key, value);
});
// At this point, you can then pass formData to your handler method
在此处详细了解有关Object.entries()
的信息-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries