1

変数をオブジェクト インデックスとして渡す方法を見つけようとしています。次に例を示します。

function createObject(index,value){
  var data = {index:value};
  console.log(data);
}

createObject('hello','world');

//result Object { index="world"} instead of Object { "hello"="world"}

これまでのところ、この特定の答えを見つけることができませんでした..

ありがとう!

4

3 に答える 3

8

object[index]次の表記法を使用できます。

function createObject(index,value){
    var data = {};
    data[index] = value;
    console.log(data);
}
于 2013-10-04T12:30:05.180 に答える
0

あなたが何を望んでいるのか理解できたら、それがコードです

function createObject(value){
  var data = {'index':value};
  console.log(data);
}

createObject('world');
于 2013-10-04T12:31:22.733 に答える
0
function createObject(index, value) {
    var data = { 'index' : value };
    console.log(data);
}

createObject('hello', 'world');
于 2013-10-04T12:34:25.347 に答える