lk09364
2015-05-15 08:11:47 +08:00
先去找提问的智慧看看。
然后,如果我没理解错的话,你有一个 array ,如下:
```
var arr = ['key','value','john','JOHN','doe','DOE','foo','FOO','bar','BAR'];
```
你希望输出如下的 array:
```
[{"key": "john", "value": "JOHN"}, {"key": "doe", "value": "DOE"}, {"key": "foo", "value": "FOO"}, {"key": "bar", "value": "BAR"}]
```
如果上述无误,你可以这样做:
```
var k1 = arr[0], k2 = arr[1], result = [];
for (var i = 2; i < arr.length; i+=2) {
var obj = {};
obj[k1] = arr[i];
obj[k2] = arr[i+1];
result.push(obj);
}
```