Last active
August 29, 2015 13:58
-
-
Save memememomo/10340683 to your computer and use it in GitHub Desktop.
inputのnameで配列表記されているものをjavascriptのデータ構造に変換する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var is_array = function(value) { | |
return value && | |
typeof value === 'object' && | |
typeof value.length === 'number' && | |
typeof value.splice === 'function' && | |
!(value.propertyIsEnumerable('length')); | |
} | |
function parse_param(params) { | |
var build_hash; | |
build_hash = function (hash, value, list) { | |
if (list.length == 1) { | |
if (is_array(value) && list[0] == "") { | |
for (var i=0; i < value.length; i++) { | |
hash[i] = value[i]; | |
} | |
} else { | |
hash[list[0]] = value; | |
} | |
return hash; | |
} | |
var key = list.shift(); | |
if (!hash[key]) hash[key] = {}; | |
hash[key] = build_hash(hash[key], value, list); | |
return hash; | |
}; | |
ret = {}; | |
for (key in params) { | |
if (key.match(/^(.+?)(\[.*?\]+)$/)) { | |
name = RegExp.$1 | |
array = RegExp.$2; | |
value = params[key]; | |
var index = []; | |
var buf = array; | |
while(1) { | |
if (buf.match(/^\[(.*?)\](\[.*?\]*)*$/)) { | |
cur = RegExp.$1; | |
buf = RegExp.$2; | |
index.push(cur); | |
} else { | |
break; | |
} | |
} | |
index.unshift(name); | |
ret = build_hash(ret, value, index); | |
} else { | |
ret[key] = params[key]; | |
} | |
} | |
return ret; | |
} | |
var params; | |
var ret; | |
params = { | |
'list[0]': 'test1', | |
'list[1]': 'test2' | |
}; | |
ret = parse_param(params); | |
console.log(ret['list'][0]); // test1 | |
console.log(ret['list'][1]); // test2 | |
params = { | |
'schedule[starttime][0]': '00', | |
'schedule[starttime][1]': '01', | |
'schedule[endtime][0]': '10', | |
'schedule[endtime][1]': '11' | |
}; | |
ret = parse_param(params); | |
console.log(ret['schedule']['starttime'][0]); // '00'; | |
console.log(ret['schedule']['starttime'][1]); // '01'; | |
console.log(ret['schedule']['endtime'][0]); // '10'; | |
console.log(ret['schedule']['endtime'][1]); // '11'; | |
params = { | |
'schedule[starttime][0]': '00', | |
'schedule[starttime][1]': '01', | |
'schedule[endtime][0]': '10', | |
'schedule[endtime][1]': '11', | |
'name': 'uhouho' | |
}; | |
ret = parse_param(params); | |
console.log(ret['schedule']['starttime'][0]); // '00'; | |
console.log(ret['schedule']['starttime'][1]); // '01'; | |
console.log(ret['schedule']['endtime'][0]); // '10'; | |
console.log(ret['schedule']['endtime'][1]); // '11'; | |
console.log(ret['name']); // 'uhouho'; | |
params = { | |
'list[0][name]': 'test1', | |
'list[0][age]': 29, | |
'list[1][name]': 'test2', | |
'list[1][age]': 30 | |
}; | |
ret = parse_param(params); | |
console.log(ret['list'][0]['name']); // test1 | |
console.log(ret['list'][0]['age']); // 29 | |
console.log(ret['list'][1]['name']); // test2 | |
console.log(ret['list'][1]['age']); // 30 | |
params = { | |
'list[]': ['test1', 'test2'] | |
}; | |
ret = parse_param(params); | |
console.log(ret['list'][0]); // test1 | |
console.log(ret['list'][1]); // test2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment