通过索引javascript,ES6获取数组元素

时间:2017-12-06 07:12:26

标签: javascript

我有一个数组fruits = ['apple','mango','eggs','guvava','... many others']。我有一个索引15,所以我目前正在将元素作为fruits[14]

我有什么方法可以使用.splice / .slice或任何ES6方法做同样的事情。

我试过.slice(14,1)但没有得到理想的结果。

1 个答案:

答案 0 :(得分:2)

fruits.slice(14,15)[0] // => get you the value of fruits[14] and allocate new memory for a new array

fruits.splice(14,1)[0] // => get you fruits[14] but mutates fruits array and also allocate new memory for a new array