4

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' }
  ];
5
  • 4
    Why not start with for(var i =1; i < people.length; i++) { in your loop? Commented Apr 17, 2019 at 7:30
  • 1
    you could slice the array, like here: stackoverflow.com/q/42374873/1447675 Commented Apr 17, 2019 at 7:30
  • 1
    what is expected output? 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 Commented Apr 17, 2019 at 7:38
  • Updated my question Commented Apr 17, 2019 at 7:59

5 Answers 5

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);

Sign up to request clarification or add additional context in comments.

2 Comments

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
@Mario Should meet your requirements now.
2

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

Comments

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)

7 Comments

I want to leave only { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } in people array
the new array that is been created called multipleDemo doesn't have { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' }. I think this is what you wanted. Check the multipleDemo again
people array must to have only one element { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' }
okay!. So you only want { name: 'Adam', email: '[email protected]', age: 12, country: 'United States' } to be in the array. rest of the elements you don't want. right?
Updated my question. Rest of the elements put in multipleDemo array
|
0

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);
});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.