如何在 JS 中完成 PHP 中的等价物?:
$this->gridColumnData[] = array('field' => 'id', 'width' => 50, 'title' => 'Enquiry Id') ;
$this->gridColumnData[] = array('field' => 'Name', 'width' => 90, 'title' => 'Name') ;
我知道如何创建一个简单的数组,例如:
var myArray = [[23, 45, 12], ['stringA', 'stringB']] ;
但是有没有可能做这样的事情:
var myArray = ["numbers" => ["A" => 23, "B" => 45, "C" => 12], "strings" => ["firstone" => 'stringA', "secondone" => 'stringB']];
最佳答案
您所描述的是一个对象文字,一个异构键值对的容器。在这种情况下,我们必须将其与数组结合起来才能获得您描述的行为:
var myArray = {
numbers: [{ A: 23, B: 45, C: 12 }],
strings: { firstone: "stringA", secondone: "stringB" }
};
例。要访问对象中的 B
属性,您必须执行 myArray.numbers[0].B
。要获取 "secondone"
的值,请执行 myArray.strings.secondone
。
关于php - Javascript 中的关联子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14911035/