javascript - 如何使用javascript在空数组中的特定索引处插入文件对象?

标签 javascript arrays

enter image description here我需要在特定索引处插入一个文件对象,例如:使用 angularJs 在空数组中的 1,5,6。我想要如下附图中给出的结果

enter image description here

最佳答案

您可以只使用[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/

相关文章:

javascript - 在 React 中将对象从数组移动到数组到另一个对象时镜像位置

javascript - 在 JavaScript 中按语言检索元标记的内容

php - 使用 PHP 关联数组创建多个 JSON 对象

java - 如何修复java中随机数 boolean 数组的: Exception in thread "main" java. lang.ArrayIndexOutOfBoundsException

javascript - 在路由中运行 .run 时从 Angularjs 中的 url 中删除 #

javascript - 与 .find() 相反

javascript - 如何动态加载javascript文件?

c - 在 C 中传递数组、指向 int 的指针

javascript - 从字符和数字数组中查找最大值

c++ - 检查数组中消息的最有效方法