我正在發送一個陣列物件,它現在在我的控制臺中看起來像這樣:
[{
}]
但我想要這樣:
{
}
有人可以告訴我如何將其更改為我想要的形式嗎?這是我的檔案陣列的外觀:
documentsArray = [];
這就是我的代碼的樣子:
uploadFile2(files: FileList | null): void {
const file = files.item(0)
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result})
console.log(this.documentsArray)
}
}
uj5u.com熱心網友回復:
您可以使用索引位置訪問陣列項:
const foo = [{ bar: 'baz' }, { quz: 'qux' }];
console.log(foo[0]); // only first item: `{ bar: 'baz' }`
IE:
uploadFile2(files: FileList | null): void {
const file = files.item(0)
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result});
// you can use the first object in the array
console.log(this.documentsArray[0]);
}
}
uj5u.com熱心網友回復:
JSON 規范是這樣的:
[{ array1 }, {array2}, {other arrays}]
這就是標準的whay,你為什么要跳出標準的方法??請注意,以后如果你想用其他平臺交換資料,你將重建everithing,因為json陣列是[],如果你沒有它,json解碼將失敗,如果你不知道如何處理[ ] 只是臨時洗掉寫入/讀取字串中的第一個和最后一個字符,但不是那么好..
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/355032.html
標籤:javascript 有角的 打字稿
