Javascript 通过匹配前缀对对象元素进行排序

Javascript 通过匹配前缀对对象元素进行排序,javascript,arrays,json,sorting,object,Javascript,Arrays,Json,Sorting,Object,我有一个像下面这样的物体 let fragment = { "lcxsm-item-1-heading": "Romeo", "lcxsm-item-1-heading-1-subheading": "Lima", "lcxsm-item-1-heading-2-subheading": "Zero", "lcxsm-item-1-he

我有一个像下面这样的物体

let fragment = {
    "lcxsm-item-1-heading": "Romeo",
    "lcxsm-item-1-heading-1-subheading": "Lima",
    "lcxsm-item-1-heading-2-subheading": "Zero",
    "lcxsm-item-1-heading-3-subheading": "France",
    "lcxsm-item-1-heading-1-subheading-1-subitem": "Jupiter",
    "lcxsm-item-1-heading-1-subheading-2-subitem": "Sunday",
    "lcxsm-item-1-heading-1-subheading-3-subitem": "Match",

    "lcxsm-item-2-heading": "email",
    "lcxsm-item-2-heading-1-subheading": "password",
    "lcxsm-item-2-heading-2-subheading": "user",
    "lcxsm-item-2-heading-3-subheading": "Italia",

    "lcxsm-item-3-heading": "you",
    "lcxsm-item-3-heading-1-subheading": "Rome",
    "lcxsm-item-3-heading-2-subheading": "Indiana",
    "lcxsm-item-3-heading-3-subheading": "phone",
    "lcxsm-item-3-heading-1-subheading-1-subitem": "number",
    "lcxsm-item-3-heading-1-subheading-2-subitem": "mail",
    "lcxsm-item-3-heading-1-subheading-3-subitem": "id"
}
我正在尝试创建一个名为“getObjectListWithPrefix”的函数,该函数提供如下输出

[
{
“主标题”:“罗密欧”,
“副标题”:[“利马”、“零”、“法国”],
“子项”:[“木星”、“周日”、“比赛”]
},
{
“主标题”:“电子邮件”,
“副标题”:[“密码”、“用户”、“斜体”]
},
{
“主标题”:“你”,
“副标题”:[“罗马”、“印第安纳”、“电话”],
“子项”:[“编号”、“邮件”、“id”]
}
]
我正在使用函数调用
getObjectListWithPrefix(“lcxsm项目-”,片段,“-标题”)
仅限。是否可以仅使用前缀和后缀在对象中循环?有时每个子目可能有标题、子目和分项。有人能帮我吗???

首先,您需要从键字符串中提取数据,然后对它们进行分组

let fragment={“lcxsm-item-1-标题”:“罗密欧”、“lcxsm-item-1-标题-1-子目”:“利马”、“lcxsm-item-1-标题-2-子目”:“零”、“lcxsm-item-1-标题-3-子目”:“法国”、“lcxsm-item-1-标题-1-子目-1-子目”:“木星”、“lcxsm-item-1-标题-1-子目-2-子目”:“周日”,“lcxsm-item-1-标题-1-子目-3-子目”:“匹配”,“lcxsm-item-2-标题”:“电子邮件”,“lcxsm-item-2-标题-1-子目”:“密码”,“lcxsm-item-2-标题-2-子目”:“用户”,“lcxsm-item-2-标题-3-子目”:“意大利”,“lcxsm-item-3-标题”:“您”,“lcxsm-item-3-标题-1-子目”:“罗马”,“lcxsm-item-3-标题-2-子目”:”印第安纳州,“lcxsm-item-3-标题-3-子目”:“电话”,“lcxsm-item-3-标题-1-子目-1-子项”:“号码”,“lcxsm-item-3-标题-1-子目-2-子项”:“邮件”,“lcxsm-item-3-标题-1-子目-3-子项”:“id”
让结果=[];
for(片段中的常量键){
设keys=key_str.split(“-”);
设titel=keys[keys.length-1];
设索引=键[2]-1;
let value=fragment[key_str];
如果(滴度=“标题”){
结果[索引]={“主标题”:值}
持续
}
结果[指标]??={};
结果[指数][滴度]??=[]
结果[索引][滴度]。推送(值)
}

console.log(result)
首先需要从键字符串中提取数据,然后对它们进行分组

let fragment={“lcxsm-item-1-标题”:“罗密欧”、“lcxsm-item-1-标题-1-子目”:“利马”、“lcxsm-item-1-标题-2-子目”:“零”、“lcxsm-item-1-标题-3-子目”:“法国”、“lcxsm-item-1-标题-1-子目-1-子目”:“木星”、“lcxsm-item-1-标题-1-子目-2-子目”:“周日”lcxsm-item-1-标题-1-子目-3-子目“:“匹配”,“lcxsm-item-2-标题“:“电子邮件”,“lcxsm-item-2-标题-1-子目“:“密码”,“lcxsm-item-2-标题-2-子目“:“用户”,“lcxsm-item-2-标题-3-子目“:“意大利”,“lcxsm-item-3-标题“:“您”,“lcxsm-item-3-标题-1-子目“:“罗马”,“lcxsm-item-3-标题-2-子目“:”印第安纳州,“lcxsm-item-3-标题-3-子目”:“电话”,“lcxsm-item-3-标题-1-子目-1-子项”:“号码”,“lcxsm-item-3-标题-1-子目-2-子项”:“邮件”,“lcxsm-item-3-标题-1-子目-3-子项”:“id”
让结果=[];
for(片段中的常量键){
设keys=key_str.split(“-”);
设titel=keys[keys.length-1];
设索引=键[2]-1;
let value=fragment[key_str];
如果(滴度=“标题”){
结果[索引]={“主标题”:值}
持续
}
结果[指标]??={};
结果[指数][滴度]??=[]
结果[索引][滴度]。推送(值)
}

console.log(result)
在您的问题中包括您尝试过的代码,在您的问题中包括您尝试过的代码