最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what is the equivalent of substring for an array? - Stack Overflow

programmeradmin2浏览0评论

I simply want to extract the values of an array from [1] to the end. Is this the only (or best) way to do it?

// example with a string
var stringy = "36781"
console.log(stringy.substring(1))

// example with an array
var array2 = [3,6,7,8,1]
array2.shift()
console.log(array2)

I simply want to extract the values of an array from [1] to the end. Is this the only (or best) way to do it?

// example with a string
var stringy = "36781"
console.log(stringy.substring(1))

// example with an array
var array2 = [3,6,7,8,1]
array2.shift()
console.log(array2)
Share Improve this question asked Aug 25, 2013 at 0:09 dwilbankdwilbank 2,5202 gold badges28 silver badges41 bronze badges 4
  • 1 You probably want to use slice. – Waleed Khan Commented Aug 25, 2013 at 0:11
  • slice is it - thanks Mr. Khan – dwilbank Commented Aug 25, 2013 at 0:14
  • If you are ok with mutating the original shift works, if you want a new version slice(1); if you want a really bad implementation for this use case: filter :) – dc5 Commented Aug 25, 2013 at 0:15
  • Read documentation when you need to know what methods are available: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – user2437417 Commented Aug 25, 2013 at 0:19
Add a ment  | 

1 Answer 1

Reset to default 8
var array2 = [3,6,7,8,1];
array2.slice(1);

will produce [6, 7, 8, 1]

Note that slice copies the array's contents only one level deep. What that means technically is that nested arrays or objects within the output of slice are still references to the same arrays or objects contained in the original parent array. What that means to you practically is that changing an element or property in the nested arrays/objects returned from slice will also change the same element or property within the nested array or object contained in the original parent array.

发布评论

评论列表(0)

  1. 暂无评论