admin管理员组文章数量:1026661
Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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);
}
Example code:
I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' }
];
Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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);
}
Example code: https://plnkr.co/edit/UJfRUs6dAT1NC1EnOvqA?p=preview
I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' }
];
Share
Improve this question
edited Apr 17, 2019 at 7:56
asked Apr 17, 2019 at 7:28
user8777652user8777652
5
-
4
Why not start with
for(var i =1; i < people.length; i++) {
in your loop? – Mark Commented Apr 17, 2019 at 7:30 - 1 you could slice the array, like here: stackoverflow./q/42374873/1447675 – Nina Scholz Commented Apr 17, 2019 at 7:30
- 1 what is expected output? – brk Commented Apr 17, 2019 at 7:31
- I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo – user8777652 Commented Apr 17, 2019 at 7:38
- Updated my question – user8777652 Commented Apr 17, 2019 at 7:59
5 Answers
Reset to default 4You can use Array.prototype.slice()
to modify your arrays to get your desired output.
let people = [{
name: 'Adam',
email: '[email protected]',
age: 12,
country: 'United States'
},
{
name: 'Amalie',
email: '[email protected]',
age: 12,
country: 'Argentina'
},
{
name: 'Estefanía',
email: '[email protected]',
age: 21,
country: 'Argentina'
},
{
name: 'Adrian',
email: '[email protected]',
age: 21,
country: 'Ecuador'
},
{
name: 'Wladimir',
email: '[email protected]',
age: 30,
country: 'Ecuador'
},
{
name: 'Samantha',
email: '[email protected]',
age: 30,
country: 'United States'
},
{
name: 'Nicole',
email: '[email protected]',
age: 43,
country: 'Colombia'
},
{
name: 'Natasha',
email: '[email protected]',
age: 54,
country: 'Ecuador'
},
{
name: 'Michael',
email: '[email protected]',
age: 15,
country: 'Colombia'
},
{
name: 'Nicolás',
email: '[email protected]',
age: 43,
country: 'Colombia'
}
];
let multipleDemo = people.slice(1);
people = people.slice(0, 1);
console.log(multipleDemo);
console.log('--------------------');
console.log(people);
You can use Array Destructuring to unpack and assign remaining part of the array to a variable using rest pattern and use .forEach()
to iterate over them as follows:
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; }
There are many ways to achieve this. but the easiest and concise solution would be using the filter()
. Which returns an array that contains each element where the condition is met.
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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)
You can use .slice()
to omit n elements from the beginning.
Array.slice(1)
means you take the array starting from index 1
to the end.
You can also define until which element you want to slice off.
Array.slice(1, 3)
will slice the elements of index 1
and 2
. It will be Amalie and Estefanía in this case.
let people = [
{ name: "Adam", email: "[email protected]", age: 12, country: "United States" },
{ name: "Amalie", email: "[email protected]", age: 12, country: "Argentina" },
{ name: "Estefanía", email: "[email protected]", age: 21, country: "Argentina" },
{ name: "Adrian", email: "[email protected]", age: 21, country: "Ecuador" },
{ name: "Wladimir", email: "[email protected]", age: 30, country: "Ecuador" },
{ name: "Samantha", email: "[email protected]", age: 30, country: "United States" },
{ name: "Nicole", email: "[email protected]", age: 43, country: "Colombia" },
{ name: "Natasha", email: "[email protected]", age: 54, country: "Ecuador" },
{ name: "Michael", email: "[email protected]", age: 15, country: "Colombia" },
{ name: "Nicolás", email: "[email protected]", age: 43, country: "Colombia" }
];
let multipleDemo = people.slice(1);
multipleDemo.forEach(function (current) {
console.log(current.name);
});
Since you simply want to copy the elements from people
array to multipleDemo
array excluding the first element, you could use slice() array method.
multipleDemo = people.slice(1)
.slice(1)
will copy the contents of people
array from index1 without reference to the multipleDemo
array.
.slice() in MDN
Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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);
}
Example code:
I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' }
];
Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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);
}
Example code: https://plnkr.co/edit/UJfRUs6dAT1NC1EnOvqA?p=preview
I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' }
];
Share
Improve this question
edited Apr 17, 2019 at 7:56
asked Apr 17, 2019 at 7:28
user8777652user8777652
5
-
4
Why not start with
for(var i =1; i < people.length; i++) {
in your loop? – Mark Commented Apr 17, 2019 at 7:30 - 1 you could slice the array, like here: stackoverflow./q/42374873/1447675 – Nina Scholz Commented Apr 17, 2019 at 7:30
- 1 what is expected output? – brk Commented Apr 17, 2019 at 7:31
- I want to leave { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo – user8777652 Commented Apr 17, 2019 at 7:38
- Updated my question – user8777652 Commented Apr 17, 2019 at 7:59
5 Answers
Reset to default 4You can use Array.prototype.slice()
to modify your arrays to get your desired output.
let people = [{
name: 'Adam',
email: '[email protected]',
age: 12,
country: 'United States'
},
{
name: 'Amalie',
email: '[email protected]',
age: 12,
country: 'Argentina'
},
{
name: 'Estefanía',
email: '[email protected]',
age: 21,
country: 'Argentina'
},
{
name: 'Adrian',
email: '[email protected]',
age: 21,
country: 'Ecuador'
},
{
name: 'Wladimir',
email: '[email protected]',
age: 30,
country: 'Ecuador'
},
{
name: 'Samantha',
email: '[email protected]',
age: 30,
country: 'United States'
},
{
name: 'Nicole',
email: '[email protected]',
age: 43,
country: 'Colombia'
},
{
name: 'Natasha',
email: '[email protected]',
age: 54,
country: 'Ecuador'
},
{
name: 'Michael',
email: '[email protected]',
age: 15,
country: 'Colombia'
},
{
name: 'Nicolás',
email: '[email protected]',
age: 43,
country: 'Colombia'
}
];
let multipleDemo = people.slice(1);
people = people.slice(0, 1);
console.log(multipleDemo);
console.log('--------------------');
console.log(people);
You can use Array Destructuring to unpack and assign remaining part of the array to a variable using rest pattern and use .forEach()
to iterate over them as follows:
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; }
There are many ways to achieve this. but the easiest and concise solution would be using the filter()
. Which returns an array that contains each element where the condition is met.
let people = [
{ name: 'Adam', email: '[email protected]', age: 12,
country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: '[email protected]', age: 30,
country: 'United States' },
{ name: 'Nicole', email: '[email protected]', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: '[email protected]', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: '[email protected]', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', 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)
You can use .slice()
to omit n elements from the beginning.
Array.slice(1)
means you take the array starting from index 1
to the end.
You can also define until which element you want to slice off.
Array.slice(1, 3)
will slice the elements of index 1
and 2
. It will be Amalie and Estefanía in this case.
let people = [
{ name: "Adam", email: "[email protected]", age: 12, country: "United States" },
{ name: "Amalie", email: "[email protected]", age: 12, country: "Argentina" },
{ name: "Estefanía", email: "[email protected]", age: 21, country: "Argentina" },
{ name: "Adrian", email: "[email protected]", age: 21, country: "Ecuador" },
{ name: "Wladimir", email: "[email protected]", age: 30, country: "Ecuador" },
{ name: "Samantha", email: "[email protected]", age: 30, country: "United States" },
{ name: "Nicole", email: "[email protected]", age: 43, country: "Colombia" },
{ name: "Natasha", email: "[email protected]", age: 54, country: "Ecuador" },
{ name: "Michael", email: "[email protected]", age: 15, country: "Colombia" },
{ name: "Nicolás", email: "[email protected]", age: 43, country: "Colombia" }
];
let multipleDemo = people.slice(1);
multipleDemo.forEach(function (current) {
console.log(current.name);
});
Since you simply want to copy the elements from people
array to multipleDemo
array excluding the first element, you could use slice() array method.
multipleDemo = people.slice(1)
.slice(1)
will copy the contents of people
array from index1 without reference to the multipleDemo
array.
.slice() in MDN
本文标签: javascriptIs it possible to iterate over the arrayexcluding the first elementStack Overflow
版权声明:本文标题:javascript - Is it possible to iterate over the array, excluding the first element? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745648448a2161166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论