読者です 読者をやめる 読者になる 読者になる

JavaScriptの配列を結合したくなイカ?

JavaScript



配列を結合したいでゲソ!






堅実に定義するでゲソ!

function concatenate (x, y) {
  for (var i = 0; i < y.length; i++) {
    x[x.length] = y[i];
  }
  return x;
}
> concatenate(concatenate([1, 2, 3], [4, 5, 6]), ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



pushしなイカ?

function concatenate (x, y) {
  for (var i = 0; i < y.length; i++) {
    x.push(y[i]);
  }
  return x;
}
> concatenate(concatenate([1, 2, 3], [4, 5, 6]), ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]





arguments使わなイカ?

function concatenate () {
  var a = [];
  for (var i = 0; i < arguments.length; i++) {
    for (var j = 0; j < arguments[i].length; j++) {
      a.push(arguments[i][j]);
    }
  }
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



jQueryのソーススタイルでゲソ!

function concatenate () {
	var args = arguments,
		length = args.length,
		i,
		j,
		l,
		a = [];
	for ( i = 0; i < length; i++ ) {
		for ( j = 0, l = args[ i ].length; j < l; j++ ) {
			a.push( args[ i ][ j ] );
		}
	}
	return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



JSLintを侵略しなイカ?

function concatenate() {
    "use strict";
    var a = [], args = arguments, i, j, k = args.length, l;
    for (i = 0; i < k; i += 1) {
        for (j = 0, l = args[i].length; j < l; j += 1) {
            a.push(args[i][j]);
        }
    }
    return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



O'REILLY教に侵略されたでゲソ!

function concatenate () {
  var a = [], args = arguments;
  for (var i = -1, k = args.length; ++i < k; ) {
    for (var j = -1, l = args[i].length; ++j < l; ) {
      a.push(args[i][j]);
    }
  }
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



forEach使ってみなイカ?

function concatenate () {
  var a = [];
  [].forEach.call(arguments, function (x) {
    x.forEach( function (y) {
      a.push(y);
    });
  });
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



jQueryが使えるじゃなイカ!

function concatenate () {
  var a = [];
  $.map(arguments, function (x) {
    $.map(x, function (y) {
      a.push(y)
    });
  });
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



コールバックの引数に気をつかうでゲソ!

function concatenate () {
  var a = [];
  $.each(arguments, function (i, x) {
    $.each(x, function (j, y) {
      a.push(y);
    });
  });
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



そろそろconcatを使いたくなったでゲソ!

function concatenate () {
  var a = [];
  $.map(arguments, function (x) {
    a = a.concat(x);
  });
  return a;
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



これでいい事に気付かなイカ?

function concatenate () {
  return $.map(arguments, function (x) { return x; });
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



気をつけるでゲソ!

function concatenate () {
  return [].map.call(arguments, function (x) { return x; });
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 'foo', 'bar' ] ]



reduceしなイカ?

function concatenate () {
  return [].reduce.call(
    arguments, function (x, y) {
      return x.concat(y);
  });
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



apply使ってみなイカ?

function concatenate () {
  return [].concat.apply([], arguments);
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



prototype.jsを使ってみなイカ?

function concatenate () {
  return Object.values(arguments).flatten();
}
> concatenate([1, 2, 3], [4, 5, 6], ['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



ズルしなイカ?

> _ = [ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]
>
>
>
>
>
> [1, 2, 3] + [4, 5, 6] + ['foo', 'bar'], _
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



前の記事で紹介したでゲソ!

Array.prototype.toString = function () {
  return this.map( function(x) {
    return JSON.stringify(x) + ','
  }).join('');
}
String.prototype.toArray = function () {
  return JSON.parse('[' + this.slice(0, -1) + ']');
}
> ([1, 2, 3] + [4, 5, 6] + ['foo', 'bar']).toArray()
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



オブジェクトも結合したくなったでゲソ!

Object.prototype.toString = function () {
  return JSON.stringify(this).slice(1, -1) + ',';
}
String.prototype.toObject = function () {
  return JSON.parse('{' + this.slice(0, -1) + '}');
}
> ({ x: 'foo' } + { y: 100 } + { z: true }).toObject()
{ x: 'foo', y: 100, z: true }



うまくいかないじゃなイカ!

Object.prototype.concat = function (x) {
  for (var i in x) {
    this[i] = x[i];
  }
  return this;
}
> ({ x: 'foo'}).concat({ y: 100 }).concat({ z: true })
{ x: 'foo', y: 100, concat: [Function], z: true }



Object.prototypeを侵略するのはヤメにしなイカ?

Object.prototype.concat = function (x) {
  for (var i in x) {
    if(x.hasOwnProperty(i)) {
      this[i] = x[i];
    }
  }
  return this;
}
> ({ x: 'foo'}).concat({ y: 100 }).concat({ z: true })
{ x: 'foo', y: 100, z: true }



意外とすごくなイカ?

Array.prototype['+'] = function (x) { return this.concat(x); }
> ([1, 2, 3]) ['+'] ([4, 5, 6]) ['+'] (['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



まるで足し算でゲソ!

Array.prototype['    +    '] = function (x) { return this.concat(x); }
> ([1, 2, 3])['    +    ']([4, 5, 6])['    +    '](['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



Haskell使えばいいんじゃなイカ?

Array.prototype['    ++    '] = function (x) { return this.concat(x); }
> ([1, 2, 3])['    ++    ']([4, 5, 6])['    ++    '](['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]



結局これでいいんじゃなイカ?

> [1, 2, 3].concat([4, 5, 6]).concat(['foo', 'bar'])
[ 1, 2, 3, 4, 5, 6, 'foo', 'bar' ]