Kategorie
Bez kategorii

array_unpack

var array_unpack = function (array) {
	if (array instanceof Array && array.length == 2)
		return array_unpack_part(array.shift(), array.shift());
	return false;
};

var array_unpack_part = function (av, ak) {
	var o = {}, a = [], ia = true;
	for (var vi = 0, vl = av.length; vi < vl; vi += 2) {
		var t = av.slice(vi, vi + 2);
		if (t[1] instanceof Array)
			o[ak[t[0]]] = array_unpack_part(t[1], ak);
		else
			o[ak[t[0]]] = ak[t[1]];
	}
	for (var oi in o) {
		if (!isNaN(parseInt(oi)) && isFinite(oi))
			a[oi] = o[oi];
		else
			ia = false;
	}
	return ia ? a : o;
};

Leave a Reply