enter image description here我需要在特定索引处插入一个文件对象,例如:使用 angularJs 在空数组中的 1,5,6。我想要如下附图中给出的结果
最佳答案
您可以只使用[n]
表示法。
const myArray = []
myArray[1] = 'something'
myArray[5] = 'inserted'
myArray[6] = 'in array'
console.log('myArray ', myArray)
如果您需要使用自定义属性名称,我建议您使用 javascript 对象。
然后你会做这样的事情:
const myObject = {}
myObject[1] = 'first file'
myObject[5] = 'second file'
myObject[6] = 'third file'
并且您始终可以像这样遍历该对象:
Object.keys(myObject).forEach(propertyName => console.log('property name ', propertyName, ' property value ', myObject[propertyName]));
关于javascript - 如何使用javascript在空数组中的特定索引处插入文件对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662321/