[Javascript] Array - Conact

For example there are two array of object and you want to print out each element, it is bad if you use two forEach method on each array:

var people1 = [
  {
    name: 'Mikko'
  },
  {
    name: 'Radu'
  }
];

var people2 = [
  {
    name: 'Jarno'
  },
  {
    name: 'Jussi'
  }
];

people1.forEach( (person) => {
  console.log(person.name);
});

people2.forEach( (person) => {
  console.log(person.name);
});

 

If use concat() can solve the problem:

var people1 = [
  {
    name: 'Mikko'
  },
  {
    name: 'Radu'
  }
];

var people2 = [
  {
    name: 'Jarno'
  },
  {
    name: 'Jussi'
  }
];

people1
  .concat(people2)
  .forEach( (person) => {
  console.log(person.name);
});

 

posted @ 2015-10-09 00:37  Zhentiw  阅读(509)  评论(0编辑  收藏  举报