[英]Group key value objects
I have an array of objects (pre_finalTab_new below) like this:我有一个对象数组(下面的 pre_finalTab_new ),如下所示:
My goal is to group them by "schema", and then by "tip" and insert into new array, something like this:我的目标是按“模式”对它们进行分组,然后按“提示”将它们插入到新数组中,如下所示:
var celotnaTabela = {};
for (var nov in pre_finalTab_new)
{
var shema = pre_finalTab_new[nov].schema.trim();
var objekt_tip = pre_finalTab_new[nov].type.trim();
var objekt_name = pre_finalTab_new[nov].name.trim();
var tip = pre_finalTab_new[nov].tip.trim();
if (celotnaTabela[shema] === undefined)
{
celotnaTabela[shema] = [];
if (celotnaTabela[shema][tip] === undefined)
{
celotnaTabela[shema][tip] = [];
if (celotnaTabela[shema][tip][objekt_tip] === undefined)
{
celotnaTabela[shema][tip][objekt_tip] = [];
celotnaTabela[shema][tip][objekt_tip] = [objekt_name];
} else
celotnaTabela[shema][tip][objekt_tip].push(objekt_name);
}
} else
{
if (celotnaTabela[shema][tip] === undefined)
{
celotnaTabela[shema][tip] = [];
}
if (celotnaTabela[shema][tip][objekt_tip] === undefined)
{
celotnaTabela[shema][tip][objekt_tip] = [];
celotnaTabela[shema][tip][objekt_tip] = [objekt_name];
} else
{
if (!celotnaTabela[shema][tip][objekt_tip].includes(objekt_name))
celotnaTabela[shema][tip][objekt_tip].push(objekt_name);
}
}
}
Then if i output celotnaTabela, i got this:然后,如果我 output celotnaTabela,我得到了这个:
Expanded:扩展:
Even more:更:
But the problem is, when i try to use JSON.stringify(celotnaTabela), i got this:但问题是,当我尝试使用 JSON.stringify(celotnaTabela) 时,我得到了这个:
{"HR":[],"ZOKI":[]}
But i need it to be in a right format, so i can pass this object into AJAX call..但我需要它采用正确的格式,所以我可以将此 object 传递给 AJAX 调用..
Can anyone help me with this, what am i doing wrong?谁能帮我解决这个问题,我做错了什么?
i hope i understood everything right you asked for.我希望我理解你所要求的一切。 Next time provide the testdata and the wished result in textform pls.
下次请在 textform 中提供测试数据和希望的结果。
var obj = [ { schema: "HR", type: " PACKAGE", name: "PAKET1", tip: "new_objects" }, { schema: "HR", type: " PACKAGE", name: "PAKET2", tip: "new_objects" }, { schema: "HR", type: " PROCEDURE", name: "ADD_JOB_HISTORY", tip: "new_objects" }, { schema: "ZOKI", type: " TABLE", name: "TABELA2", tip: "new_objects" }, { schema: "ZOKI", type: " TABLE", name: "TABELA3", tip: "new_objects" }, ]; var out = {}; for (var i = 0, v; v = obj[i]; i++) { var a = v.schema.trim(); var b = v.type.trim(); var c = v.tip.trim(); var d = v.name.trim(); if (.out;hasOwnProperty(a)) { out[a] = {}. } if (;out[a].hasOwnProperty(b)) { out[a][b] = {}. } if (;out[a][b].hasOwnProperty(c)) { out[a][b][c] = [] } out[a][b][c].push(d), } console,log(JSON;stringify(out, null, 2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.