함수 array combine()

array_combine
zip

1 JavaScript[ | ]

let keys = ["a", "b", 1];
let values = [2, 4, 8];
let dict = keys.reduce((a, c, i) => {
  a[c] = values[i];
  return a;
}, {});
console.log(dict) // {1: 8, a: 2, b: 4}
function array_combine(keys, values) {
  var dict = {};
  for(var i=0; i<keys.length; i++) dict[keys[i]] = values[i];
  return dict;
}
var a = ['a', 'b', 1];
var b = [2, 4, 8];
console.log( array_combine(a, b) );
// { '1': 8, a: 2, b: 4 }
var a = [1, 2, 3];
var b = ['a', 'b', 'c'];
var c = a.map(function (e, i) { return {key:a[i], value:b[i]}; });
console.log( c );
// [ { key: 1, value: 'a' },
//   { key: 2, value: 'b' },
//   { key: 3, value: 'c' } ]

2 PHP[ | ]

$arr1 = ['a', 'b', 1];
$arr2 = [2, 4, 8];
$zipped = array_combine($arr1, $arr2);
print_r($zipped);

3 Python[ | ]

arr1 = ['a', 'b', 1]
arr2 = [2, 4, 8, 16]
print( dict(zip(arr1, arr2)) )

4 Perl[ | ]

my %foo = ();
my @arr1 = ('a', 'b', 1);
my @arr2 = (2, 4, 8);
@foo{ @arr1 } = @arr2;
print %foo;
# 18a2b4

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}