是否可以遍历数组,但不包括第一个元素(忽略数组中的第一个对象)?
代码:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: 'adam@email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie@email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania@email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian@email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir@email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha@email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole@email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha@email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael@email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas@email.com', age: 43,
country: 'Colombia' }
];
for(var i =0; i < people.length; i++) {
multipleDemo.push(people[i]);
people.splice(people[i], 1000);
console.log(multipleDemo);
console.log(people);
}
示例代码:https://plnkr.co/edit/UJfRUs6dAT1NC1EnOvqA?p=preview
我想离开{名称:'Adam',电子邮件:'adam@email.com',年龄:12,国家/地区:'United States'}。我想放入arrayDemoDemo
数组中的其余元素我想要诸如FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: 'amalie@email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania@email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian@email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir@email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha@email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole@email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha@email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael@email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas@email.com', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: 'adam@email.com', age: 12,
country: 'United States' }
];
答案 0 :(得分:3)
您可以使用Array.prototype.slice()
修改数组以获得所需的输出。
let people = [{
name: 'Adam',
email: 'adam@email.com',
age: 12,
country: 'United States'
},
{
name: 'Amalie',
email: 'amalie@email.com',
age: 12,
country: 'Argentina'
},
{
name: 'Estefanía',
email: 'estefania@email.com',
age: 21,
country: 'Argentina'
},
{
name: 'Adrian',
email: 'adrian@email.com',
age: 21,
country: 'Ecuador'
},
{
name: 'Wladimir',
email: 'wladimir@email.com',
age: 30,
country: 'Ecuador'
},
{
name: 'Samantha',
email: 'samantha@email.com',
age: 30,
country: 'United States'
},
{
name: 'Nicole',
email: 'nicole@email.com',
age: 43,
country: 'Colombia'
},
{
name: 'Natasha',
email: 'natasha@email.com',
age: 54,
country: 'Ecuador'
},
{
name: 'Michael',
email: 'michael@email.com',
age: 15,
country: 'Colombia'
},
{
name: 'Nicolás',
email: 'nicolas@email.com',
age: 43,
country: 'Colombia'
}
];
let multipleDemo = people.slice(1);
people = people.slice(0, 1);
console.log(multipleDemo);
console.log('--------------------');
console.log(people);
答案 1 :(得分:1)
您可以使用Array Destructuring进行解包,并使用rest模式将数组的其余部分分配给变量,然后使用.forEach()
对其进行迭代,如下所示:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const [first, ...rest] = arr;
rest.forEach(v => console.log(v));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
有很多方法可以实现这一目标。但是最简单明了的解决方案是使用filter()
。它返回一个数组,其中包含满足条件的每个元素。
let people = [
{ name: 'Adam', email: 'adam@email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie@email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania@email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian@email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir@email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha@email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole@email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha@email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael@email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas@email.com', age: 43,
country: 'Colombia' }
];
let multipleDemo = people.filter((v, k) => k !== 0);
people = people.filter((v, k) => k === 0);
console.log(multipleDemo)
console.log(people)
答案 3 :(得分:0)
您可以使用.slice()
从一开始就省略 n 个元素。
select *
from student s
join studgroups sg on (s.studygroups = sg.studgroups_number)
join study st on (sg.studgroupys_id = st.study_studgroups_id)
where sg.studgroups_year !=s.study_kurs
and s.study_state_id IN (1,2,3,4,5,6,7,21,22,23,24);
表示您将数组从索引Array.slice(1)
开始到结尾。
您还可以定义要分割的元素。
1
将对索引Array.slice(1, 3)
和1
的元素进行切片。在这种情况下将是Amalie和Estefanía。
2
答案 4 :(得分:0)
由于您只是想将元素从people
数组复制到multipleDemo
数组(不包括第一个元素),因此可以使用slice()数组方法。
multipleDemo = people.slice(1)
.slice(1)
将从索引1复制people
数组的内容,而不引用multipleDemo
数组。
.slice() in MDN