Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to iterate over the array, excluding the first element?

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' }
  ];

1 Answers

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);
like image 111
CodeF0x Avatar answered Sep 18 '25 10:09

CodeF0x