有時候需要把js多層複雜物件陣列新增或修改部分字段,可以採用如下的兩種方法。
1)深拷貝法
//以下**通過深拷貝複製,不會改變原res.data.equipmentdata物件
const newequipmentdata = res.data.equipmentdata.map(eq => ;
neweq.modules = neweq.modules.map(m=>;
newm.fields = newm.fields.map((f,i)=>//新增乙個虛擬欄位index,用於防止table出現重名key錯誤
return newfield;
return newm;
return neweq;
2)淺拷貝法
//以下**通過淺拷貝複製,會同時改變原res.data.equipmentdata物件
const newequipmentdata = res.data.equipmentdata.map(eq => //新增乙個虛擬欄位index,用於防止table出現重名key錯誤
return newfield;
return m;
return eq;
3)附錄,以下是res.data.equipmentdata物件的資料結構
"equipmentdata": [
id: 1,
equipmentid:"cabinet-001",
name:"機櫃-001",
client:"雲濤生物",
gatewayid:"gateway001",
creator:"admin",
createtime:"2019-09-06 14:07:06",
equipmenttemp:"機櫃-04",
panel:"cabinet-panel-04",
description:"",
getwayfields:[
index:1,
fieldname:"fanrunning",
gatewaytag:"pe001",
unit:"℃",
index:2,
fieldname:"isdooropen",
gatewaytag:"pe002",
unit:"ma",
modules:[
index:1,
modulename:"cn037-001",
moduletemp:"cn037",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
index:2,
modulename:"cn037-002",
moduletemp:"cn037",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
index:3,
modulename:"pw716-001",
moduletemp:"pw716",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
fieldname:"runningoffan",
datatype:"boolean",
bytecount:1,
unit:"",
id: 2,
equipmentid:"cabinet-002",
name:"機櫃-002",
client:"雲濤生物",
gatewayid:"gateway002",
creator:"admin",
createtime:"2019-09-06 14:07:06",
equipmenttemp:"機櫃-08",
panel:"cabinet-panel-06",
description:"",
getwayfields:[
index:1,
fieldname:"fanrunning",
gatewaytag:"pe001",
unit:"℃",
index:2,
fieldname:"isdooropen",
gatewaytag:"pe002",
unit:"ma",
modules:[
index:1,
modulename:"cn037-001",
moduletemp:"cn037",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
index:2,
modulename:"cn037-002",
moduletemp:"cn037",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
index:3,
modulename:"pw716-001",
moduletemp:"pw716",
fields:[
fieldname:"toplefttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"toprighttemp",
datatype:"real",
bytecount:2,
unit:"℃",
fieldname:"current",
datatype:"real",
bytecount:2,
unit:"ma",
fieldname:"runningoffan",
datatype:"boolean",
bytecount:1,
unit:"",
js 實現 多層級物件合併
需求是使用js對資料的格式進行轉換 把乙個二維陣列 包含層級資訊,層級數是不固定的 list 轉換為多層級的物件 我的思路就是 迴圈先把list裡單條資訊轉換為 多層級物件 然後和上一條 進行合併 原資料型別 分別轉換為 a a b 最終合併效果 a2 b onsubmit void 存放的原始資料...
js多層物件陣列 合併 關於js物件陣列的合併問題
怎麼樣才能把 var dd linenid 2222 attributesonname 布料 linenid 11111 attributesonname 布料 linenid 333 attributesonname 古田路 linenid linenson002111 attributesonn...
js多層物件陣列 合併 JS物件陣列合併去重?
陣列去重,一般會在面試的時候才會碰到,要求手寫陣列去重方法的 如果是被提問到,陣列去重的方法有哪些?你能答出其中的10種,面試官很有可能對你刮目相看。在實際專案中碰到的陣列去重,一般都是後台去處理,很少讓前端處理陣列去重。雖然運用到的概率較低,但還是需要了解一下,以防面試的時候被問到。陣列去重的方法...