最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Is it possible to iterate over the array, excluding the first element? - Stack Overflow

matteradmin10PV0评论

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
Add a ment  | 

5 Answers 5

Reset to default 4

You 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

Post a comment

comment list (0)

  1. No comments so far