[英]how can I spread these arrays properly?
我需要插入一個空項目作為數據數組中的第一項,該數據將被數據綁定到下拉列表。 我的代碼如下所示:
let suppliers = response.data[0];
let defaultSupplier = [{ SupplierId:0, ContactFirstName:''}];
suppliers = [...defaultSupplier, suppliers];
我在想這段代碼會用一個默認項目初始化我的數組,然后我可以簡單地將檢索到的供應商數據傳播到這個數組中。 但是,這種方法的結果是我有一個數組,其中 defaultSupplier 是數組中的第一項,然后數組中的第二項是檢索到的供應商數據的數組。 我需要采取什么方法才能將檢索到的供應商數據附加到與 defaultSupplier 相同的級別?
你必須分散suppliers
。 但是你用一個元素創建了新的數組defaultSupplier
並傳播了這個元素。 正確的變體:
let suppliers = response.data[0]; // array
let defaultSupplier = { SupplierId:0, ContactFirstName:''};
suppliers = [defaultSupplier, ...suppliers]; // spread array suppliers
defaultSupplier
和supplier
都是對象數組。
所以,你需要像這樣將兩個數組傳播到新數組中
suppliers = [...defaultSupplier, ...suppliers];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.