我在删除数组的第一个元素时遇到麻烦如果我尝试切片(1,1)或移位我无法得到一个列表。
例如,
我的数组:[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"]
我想删除时间戳“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"]
如果我尝试切片,我只获得一个值"54:52:00:78:b8:51"
并且通过筛选得到1499783769720
。
我怎么能这样做?
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;
}
答案 0 :(得分:2)
您可以使用Array.prototype.shift():shift()方法从数组中删除第一个元素并返回该元素。此方法更改数组的长度。
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; }
答案 1 :(得分:1)
尝试理解这个例子:
var list = ["a","b","c","d"]
list = list.slice(1);
console.log(list);
切片函数不会更改原始数组,因此您需要重新分配数组变量。
答案 2 :(得分:0)
尝试.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);
答案 3 :(得分:0)
myArray.splice(myArray.indexOf(myArray[0]),1);
试试这个。 plnkr:plnkr.co/edit/tFEbwbFEyVX6TarlZJ2d?p=preview 使用这种方法,您可以传递确切的值并完全删除您需要的内容。