有以下查询结果:(key1和key2可以是任何文本)
id key1 key2 value
1 fred apple 2
2 mary orange 10
3 fred banana 7
4 fred orange 4
5 sarah melon 5
...
Run Code Online (Sandbox Code Playgroud)
我希望将数据存储在网格中(可能作为一个数组)循环所有记录,如下所示:
apple orange banana melon
fred 2 4 7 -
mary - 10 - -
sarah - - - 5
Run Code Online (Sandbox Code Playgroud)
在PHP中,使用关联数组非常简单:
$result['fred']['apple'] = 2;
Run Code Online (Sandbox Code Playgroud)
但在像这样的JavaScript关联数组中不起作用.阅读了大量的教程后,我能得到的就是:
arr=[];
arr[1]['apple'] = 2;
Run Code Online (Sandbox Code Playgroud)
但arr['fred']['apple'] = 2;不起作用.我尝试过对象数组,但对象属性不能是自由文本.我阅读教程越多,我就越困惑......
欢迎任何想法:)