我有一個物件,我可以嘗試將這個物件轉換為一個特殊的陣列,
這是索引
const index = 3
這是我的物件
object = { fruit: apple vegetable: carrot number: 5 color: red }
這是我要實作的陣列結構
[ 3: { fruit: apple vegetable: carrot number: 5 color: red } ]
我不是陣列專家,但我嘗試了這個并且不使用特殊結構轉換陣列
this.object.entries(newObj).map(x => { return index[x] })
請你能幫助我,我非常感謝你的幫助。
uj5u.com熱心網友回復:
生成具有所需長度的新陣列并將物件分配給所需的索引
const index = 3
const myObject = { fruit: 'apple', vegetable: 'carrot', number: 5, color: 'red' }
const op = new Array(index 1);
op[index] = myObject;
uj5u.com熱心網友回復:
嘗試這個:-
let index = 1;
let object = { fruit: 'apple', vegetable: 'carrot', number: 5, color: 'red' }
let array = [];
array.push(object)
console.log(array);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/447634.html
