vue3-reactive定義的對象數(shù)組如何賦值
vue3-reactive定義的對象數(shù)組賦值
type childEditQosItem = {
objectName: string;
attribute: string;
type: string;
valueType: string;
value: string;
};
const ruleList = reactive<childEditQosItem>([
{ objectName: "", attribute: "", type: "", valueType: "", value: "" },
]);
const data = [
{ attribute: "2", type: "3", valueType: "4", value: "6" },
{ objectName: "7", attribute: "8", type: "9", valueType: "" },
];將 data 數(shù)組中的元素補充完整并更新到 ruleList 中。
思路
- 類型定義:和之前一樣,先定義了
childEditQosItem類型,明確每個元素應(yīng)該包含的屬性。 - 創(chuàng)建響應(yīng)式數(shù)組
ruleList:使用reactive函數(shù)創(chuàng)建響應(yīng)式數(shù)組并初始化了一個默認元素。 - 清空
ruleList:使用splice方法將ruleList中的元素清空,為后續(xù)添加處理后的數(shù)據(jù)做準備。
使用 for...in 處理數(shù)據(jù):
- 外層
for循環(huán)遍歷data數(shù)組中的每個元素。 - 對于每個元素,先創(chuàng)建一個默認的
newItem對象,其屬性都初始化為空字符串。 - 內(nèi)層
for...in循環(huán)遍歷當(dāng)前元素的每個屬性。使用Object.prototype.hasOwnProperty.call來確保只處理對象自身的屬性,避免處理原型鏈上的屬性。 - 將當(dāng)前元素的屬性值賦值給
newItem對應(yīng)的屬性。
更新 ruleList:將處理好的 newItem 添加到 ruleList 中。
const ruleList = reactive([
{ objectName: "", attribute: "", type: "", valueType: "", value: "" },
]);
const data = [
{ attribute: "2", type: "3", valueType: "4", value: "6" },
{ objectName: "7", attribute: "8", type: "9", valueType: "" },
];
// 清空 ruleList 原有的內(nèi)容
ruleList.length = 0;
// 遍歷 data 數(shù)組
for (let i = 0; i < data.length; i++) {
const newItem = {
objectName: "",
attribute: "",
type: "",
valueType: "",
value: ""
};
// 合并 data 中當(dāng)前項的屬性到新對象
for (const key in data[i]) {
if (data[i].hasOwnProperty(key)) {
newItem[key] = data[i][key];
}
}
ruleList.push(newItem);
}
console.log(ruleList);
這個 TypeScript 錯誤提示表明,你試圖使用一個 string 類型的變量 key 來索引 newItem 對象,但 newItem 類型并沒有定義這樣的索引簽名。
要解決這個問題
你可以通過類型斷言或者使用類型守衛(wèi)來確保 key 是 newItem 對象的合法屬性名。
// 定義 childEditQosItem 類型
type childEditQosItem = {
objectName: string;
attribute: string;
type: string;
valueType: string;
value: string;
};
const ruleList = reactive<childEditQosItem[]>([
{ objectName: "", attribute: "", type: "", valueType: "", value: "" },
]);
const data = [
{ attribute: "2", type: "3", valueType: "4", value: "6" },
{ objectName: "7", attribute: "8", type: "9", valueType: "" },
];
// 清空 ruleList
ruleList.length = 0;
// 定義類型守衛(wèi)函數(shù)
function isValidKey(key: string): key is keyof childEditQosItem {
return ['objectName', 'attribute', 'type', 'valueType', 'value'].includes(key);
}
for (let i = 0; i < data.length; i++) {
const newItem: childEditQosItem = {
objectName: "",
attribute: "",
type: "",
valueType: "",
value: "",
};
for (const key in data[i]) {
if (data[i].hasOwnProperty(key) && isValidKey(key)) {
newItem[key] = data[i][key] || "";
}
}
ruleList.push(newItem);
}
console.log(ruleList);isValidKey 函數(shù)是一個類型守衛(wèi),它接受一個 string 類型的參數(shù) key,并檢查 key 是否是 childEditQosItem 類型的合法屬性名。如果是,則返回 true,并且 TypeScript 編譯器會將 key 的類型縮小為 keyof childEditQosItem。
在 for...in 循環(huán)中,使用 isValidKey(key) 作為類型守衛(wèi),確保只有合法的屬性名才能用于索引 newItem 對象。
其中
newItem[key] = data[i][key] || "";
在賦值時使用 || "" 運算符,若 data[i][key] 的值為 undefined 或者空字符串,就會將空字符串賦給 newItem[key],這樣就保證了賦值的類型是 string。
或者:
for (const key in data[i]) {
if (data[i].hasOwnProperty(key) && isValidKey(key)) {
const value = data[i][key];
if (value!== undefined) {
newItem[key] = value;
}
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中實現(xiàn)表單地區(qū)選擇與級聯(lián)聯(lián)動示例詳解
這篇文章主要為大家介紹了Vue中實現(xiàn)表單地區(qū)選擇與級聯(lián)聯(lián)動示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題
這篇文章主要介紹了vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
vue中使用echarts并根據(jù)選擇條件動態(tài)展示echarts圖表
雖然老早就看過很多echarts的例子, 但自己接觸的項目中一直都沒有真正用到過,直到最近才開始真正使用,下面這篇文章主要給大家介紹了關(guān)于vue中使用echarts并根據(jù)選擇條件動態(tài)展示echarts圖表的相關(guān)資料,需要的朋友可以參考下2023-12-12
Vue如何使用Promise.all()方法并行執(zhí)行多個請求
在Vue中,可以使用Promise.all()方法并行執(zhí)行多個異步請求,當(dāng)所有請求都成功返回時,Promise.all()將返回一個包含所有請求結(jié)果的數(shù)組,如果其中任何一個請求失敗,則會觸發(fā)catch()方法并返回錯誤信息,這種方式可以顯著提高程序的性能和響應(yīng)速度2025-01-01

