eg1(数组中包含的是数组):
var jsonData = {}; var arr = [[1, 'boy', 'dabing'], [2, 'girl', 'dabing']]; for (var i = 0; i < arr.length; i++) { var key = arr[i][1]; var value = arr[i][2]; jsonData[key] = value; } console.log(jsonData['boy'])// 'dabing'
eg2(数组中包含的是对象):
var jsonData = {}; var arr = [ { id: 3, name: 'MAN_MIDDLESCHOOL_STUDENT', value: 'predefine' }, { id: 4, name: 'FEMALE_MIDDLESCHOOL_STUDENT', value: 'predefine' }]; for (var i = 0; i < arr.length; i++) { var key = arr[i].name; var value = arr[i].value; jsonData[key] = value; } console.log(jsonData);//{ MAN_MIDDLESCHOOL_STUDENT: 'predefine', FEMALE_MIDDLESCHOOL_STUDENT: 'predefine' }
最新评论