Javascript 通过关键点将平面对象转换为嵌套结构

Javascript 通过关键点将平面对象转换为嵌套结构,javascript,Javascript,Javascript中有没有推荐的方法可以解析对象的键以将其转换为嵌套的内容?以下是一个例子: 将此转换为: { "person[profile][0][degree_type]": "Bachelor's", "person[profile][0][college_name]": "AI Miami International University of Art and Design", "person[profile][0][business_school_name]": "",

Javascript中有没有推荐的方法可以解析对象的键以将其转换为嵌套的内容?以下是一个例子:

将此转换为:

{
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
}
为此:

"person": {
  "profile": [
    {
      "degree_type": "Bachelor's",
      "college_name": "AI Miami International University of Art and Design",
      "business_school_name": "",
      "law_school_name": "",
      "other_school_name": "",
      "undergraduate_major_name": "",
      "max_gpa": ""
    },
    {
      .....
    }
  ]
}
给你!:)那很有趣

var flatObj = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var parse = function(data, string, value) {
  if (string.indexOf("]") >= 0) {
    var match = string.charAt(0) != "[" ? string.match(/([^\[]+)\[/) : string.match(/\[([^\]]+)\]/);
    var key = match[1];

    var token = key + ']';
    var index = string.indexOf(token) + token.length;

    if (!data.hasOwnProperty(key)) {
      data[key] = isNaN(key) ? {} : [];
    }

    if (index >= string.length) {
      data[key] = value;
    } else {
      parse(data[key], string.substring(index), value);
    }
  }
};

var data = {};

for (var prop in flatObj) {
  parse(data, prop, flatObj[prop]);
}
给你!:)那很有趣

var flatObj = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var parse = function(data, string, value) {
  if (string.indexOf("]") >= 0) {
    var match = string.charAt(0) != "[" ? string.match(/([^\[]+)\[/) : string.match(/\[([^\]]+)\]/);
    var key = match[1];

    var token = key + ']';
    var index = string.indexOf(token) + token.length;

    if (!data.hasOwnProperty(key)) {
      data[key] = isNaN(key) ? {} : [];
    }

    if (index >= string.length) {
      data[key] = value;
    } else {
      parse(data[key], string.substring(index), value);
    }
  }
};

var data = {};

for (var prop in flatObj) {
  parse(data, prop, flatObj[prop]);
}
给你!:)那很有趣

var flatObj = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var parse = function(data, string, value) {
  if (string.indexOf("]") >= 0) {
    var match = string.charAt(0) != "[" ? string.match(/([^\[]+)\[/) : string.match(/\[([^\]]+)\]/);
    var key = match[1];

    var token = key + ']';
    var index = string.indexOf(token) + token.length;

    if (!data.hasOwnProperty(key)) {
      data[key] = isNaN(key) ? {} : [];
    }

    if (index >= string.length) {
      data[key] = value;
    } else {
      parse(data[key], string.substring(index), value);
    }
  }
};

var data = {};

for (var prop in flatObj) {
  parse(data, prop, flatObj[prop]);
}
给你!:)那很有趣

var flatObj = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var parse = function(data, string, value) {
  if (string.indexOf("]") >= 0) {
    var match = string.charAt(0) != "[" ? string.match(/([^\[]+)\[/) : string.match(/\[([^\]]+)\]/);
    var key = match[1];

    var token = key + ']';
    var index = string.indexOf(token) + token.length;

    if (!data.hasOwnProperty(key)) {
      data[key] = isNaN(key) ? {} : [];
    }

    if (index >= string.length) {
      data[key] = value;
    } else {
      parse(data[key], string.substring(index), value);
    }
  }
};

var data = {};

for (var prop in flatObj) {
  parse(data, prop, flatObj[prop]);
}
使用ES5和方法,您可能会遇到以下情况:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});
零填充右移运算符(
>
)用于模拟JS中的内部
ToUint32
操作,以确保字符串是有效数组的索引,否则将被视为属性(例如,
“1.5”
可以解析为数字,但不是有效数组的索引)。请参见

使用ES5和方法,您可以有如下内容:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});
零填充右移运算符(
>
)用于模拟JS中的内部
ToUint32
操作,以确保字符串是有效数组的索引,否则将被视为属性(例如,
“1.5”
可以解析为数字,但不是有效数组的索引)。请参见

使用ES5和方法,您可以有如下内容:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});
零填充右移运算符(
>
)用于模拟JS中的内部
ToUint32
操作,以确保字符串是有效数组的索引,否则将被视为属性(例如,
“1.5”
可以解析为数字,但不是有效数组的索引)。请参见

使用ES5和方法,您可以有如下内容:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});

零填充右移运算符(
>
)用于模拟JS中的内部
ToUint32
操作,以确保字符串是有效数组的索引,否则将被视为属性(例如,
“1.5”
可以解析为数字,但不是有效数组的索引)。请参见

这绝对是矫枉过正,但下面是一个示例,说明如何使用传统的词法分析器对关键语法进行词法分析。这样做的好处是具有良好的错误报告功能,并且能够轻松地增强语法

下面是一个语法错误示例,您可以通过以下方式获得:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});
expandkey({“个人[档案][0]学位类型]:“学士学位”})

Uncaught错误:无效键“person[profile][0]degree_type]”;应为“[”并在字符18处看到“d”

/*jshint-esnext:true*/
var expandKeys=(函数(){
"严格使用",;
变量LBRACK='[',
RBRACK=']';
返回函数(obj){
var result={};
Object.keys(obj.forEach)(函数(k){
var令牌=来自(k)的令牌,
o=结果,
t=nextFrom(令牌,k).值;
while(t){
设next=nextFrom(令牌,k);
如果(next.done)o[t]=obj[k];
如果(o.hasOwnProperty(t))o=o[t];
else o=o[t]=isInt(+next.value)?[]:{};
t=下一个值;
}
});
返回结果;
};
函数isInt(val){
返回val>>>0==val;
}
功能下一步(gen,k){
试一试{
返回gen.next();
}捕获(e){
抛出新错误(“无效键“+k+”;“+e.message”);
}
}
函数*tokensFrom(输入){
var state=prop,
i=0,
c=输入[0],
代币
而(state=state())产生令牌;
收益券;
函数prop(){
var p='';
而(c&&c!==LBRACK&&c!==RBRACK){
p+=c;
消费();
}
if(!p)错误('需要属性名');
令牌=p;
返回c?surroundedProp:null;
}
函数surroundedProp(){
匹配(LBRACK);
prop();
匹配(RBRACK);
返回c?surroundedProp:null;
}
函数匹配(字符){
如果(c==char)consume();
else错误(“预期为“+char+”,而不是saw“+(c | |“)+””);
}
函数消耗(){
返回c=输入[++i];
}
函数错误(msg){
抛出新错误(字符'+i'处的msg+';
}
}
})();
风险值数据={
“个人[档案][0][学位类型]:“学士学位”,
《人物》〔0〕〔学院名称〕:“迈阿密国际艺术设计大学”
“个人[档案][0][business\u school\u name]:”,
“个人[档案][0][法学院名称]:”,
“个人[档案][0][其他学校名称]:”,
“人[简介][0][本科专业名称]:“人类学”,
“个人[个人资料][0][max_gpa]:”,
“个人[档案][1][学位类型]:”,
“个人[档案][1][学院名称]:”“
};
var结果=扩展键(数据),
resultJson=JSON.stringify(result,null,2),
pre=document.createElement('pre');
pre.textContent=resultJson;

document.body.appendChild(pre);
这绝对是矫枉过正,但下面是一个示例,说明如何使用传统的lexer对关键语法进行词法分析。这样做的好处是可以很好地报告错误,并且可以很容易地增强语法

下面是一个语法错误示例,您可以通过以下方式获得:

var data = {
  "person[profile][0][degree_type]": "Bachelor's",
  "person[profile][0][college_name]": "AI Miami International University of Art and Design",
  "person[profile][0][business_school_name]": "",
  "person[profile][0][law_school_name]": "",
  "person[profile][0][other_school_name]": "",
  "person[profile][0][undergraduate_major_name]": "Anthropology",
  "person[profile][0][max_gpa]": "",
  "person[profile][1][degree_type]": "",
  "person[profile][1][college_name]": ""
};

var object = Object.keys(data).reduce(function(result, item) {
  var o = result;
  var leaf = item.match(/\w+/g).reduce(function(current, next) {
    o[current] = o[current] || (String(next >>> 0) === next ? [] : {});
    o = o[current];
    return next;
  });
  o[leaf] = data[item];
  return result;
}, {});
expandkey({“个人[档案][0]学位类型]:“学士学位”})

Uncaught错误:无效键“person[profile][0]degree_type]”;应为“[”并在字符18处看到“d”

/*jshint-esnext:true*/
var expandKeys=(函数(){
"严格使用",;
变量LBRACK='[',
RBRACK=']';
返回函数(obj){
var result={};