0

I'm having troubles with removing first element of a array If i try to slice(1,1) or shift I cant get a list.

For example,

my array: [1499783769720,"54:52:00:62:46:66","54:52:00:b0:fa:57","54:52:00:8f:d9:7c","54:52:00:e7:67:10","54:52:00:26:56:56","54:52:00:33:3a:4d","54:52:00:7b:f4:ec","54:52:00:1d:48:1e","54:52:00:55:14:ed","54:52:00:78:b8:51"]

And i want to remove the timesamp "1499783769720" and just have ["54:52:00:62:46:66","54:52:00:b0:fa:57","54:52:00:8f:d9:7c","54:52:00:e7:67:10","54:52:00:26:56:56","54:52:00:33:3a:4d","54:52:00:7b:f4:ec","54:52:00:1d:48:1e","54:52:00:55:14:ed","54:52:00:78:b8:51"]

If i Try to slice I only obtain a single value "54:52:00:78:b8:51" and with sift I get 1499783769720.

How I can do this?

var randomMac = require('random-mac');
var now = Date.now();
var lista=[];

lista.push(now);
for(var i=0;i<10;i++){
    var random = randomMac();
    lista[i+1] = random;
}
3
  • lista.shift(); console.log(lista); Commented Jul 11, 2017 at 14:38
  • slice returns the element removed. An important aspect of slice is that it does not change the array which invokes it.. Commented Jul 11, 2017 at 14:38
  • Actually, "slice returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified" - Source Commented Jul 11, 2017 at 14:43

4 Answers 4

6

Try to understand this example:

var list = ["a","b","c","d"]
list = list.slice(1);
console.log(list);

slice function doesn't change the original array so you need to reassign the array variable.

Sign up to request clarification or add additional context in comments.

3 Comments

With list.shift() no need to do variable reassign.. Because shift() method removes the first element from the array
Yes, but mutating existing variables is discouraged in functional programming, because of the side-effects. Thus, slice() is preferred.
If you are looking for performance, shift is almost 10x faster than slicing.
2

You can use Array.prototype.shift(): The shift() method removes the first element from an array and returns that element. This method changes the length of the array.

var list = [1499783769720,"54:52:00:62:46:66","54:52:00:b0:fa:57","54:52:00:8f:d9:7c","54:52:00:e7:67:10","54:52:00:26:56:56","54:52:00:33:3a:4d","54:52:00:7b:f4:ec","54:52:00:1d:48:1e","54:52:00:55:14:ed","54:52:00:78:b8:51"];

list.shift(); // First removed
console.log(list);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Try .slice(1):

const firstRemoved = [
  1499783769720,
  '54:52:00:62:46:66',
  '54:52:00:b0:fa:57',
  '54:52:00:8f:d9:7c',
  '54:52:00:e7:67:10',
  '54:52:00:26:56:56',
  '54:52:00:33:3a:4d',
  '54:52:00:7b:f4:ec',
  '54:52:00:1d:48:1e',
  '54:52:00:55:14:ed',
  '54:52:00:78:b8:51'
].slice(1);

console.log(firstRemoved);

1 Comment

"An important aspect of slice is that it does not change the array which invokes it."
-1
myArray.splice(myArray.indexOf(myArray[0]),1);

Try this. plnkr: plnkr.co/edit/tFEbwbFEyVX6TarlZJ2d?p=preview With this approach you can pass the exact value and delete what you need exactly.

6 Comments

Why should this work? You should explain your answers.
I prefer to use .slice() because it doesn't mutate your array, but will give you a modified copy.
dont work, it gives me the first element
So, do you want to modify the array (then use .splice(1)) or do you want a copy of the array with the first element removed (then use .slice(1))? slice is the better approach IMHO.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.