jquerymerge 2数组深度递归
在jquery中是否可以将这些合并到数组中jquerymerge 2数组深度递归,jquery,arrays,merge,Jquery,Arrays,Merge,在jquery中是否可以将这些合并到数组中 [["add", ["image", ["to", ["title", ["published", undefined]]]]]] [["add", ["text", ["to", ["title", ["published", undefined]]]]]] 要买这个吗 ["add", ["image", ["to", ["title", ["published", undefined]]]] ["text", ["to", ["t
[["add", ["image", ["to", ["title", ["published", undefined]]]]]]
[["add", ["text", ["to", ["title", ["published", undefined]]]]]]
要买这个吗
["add",
["image", ["to", ["title", ["published", undefined]]]]
["text", ["to", ["title", ["published", undefined]]]]
]
如果找到这个(),那么发送的内容对我来说很有用,因为它只是将数组推到一起
我的想法是遍历数组递归并检查值
如果他们匹配,我会把另一个的孩子推到匹配val的索引中
但我不知道如何开始甚至结束这一切
thx家伙
解释
var komands = [];
kmd('add,image,to,title,published');
kmd('add,text,to,title,published');
function kmd(kmd){
komand = [];
kmnd = kmd.split(',').reverse();
function pusher(value, obj){obj[1]=value;return [obj.reverse()];}
$.each(kmnd,function(i,val){
komand = pusher(val, komand);
});
komands = !!!! array_merge_recursive(komands, komand); !!!!
console.log(komands);
}
我编写了这个函数,通过它,我可以在几秒钟内将自定义命令归档为一个大型树状结构。
唯一的问题是我无法将单个阵列合并到一个atm中。
这就是为什么我问这个问题,优雅的解决方案是这样的。 我使用对象而不是数组,并用深度副本扩展它们 谢谢你的建议
var komands = {};
kmd('add,image,to,published(),execute');
kmd('add,image,to,unpublished(),execute');
function kmd(kmd){
komand = {};
function pusher(v,oj){o={};o[v]=oj;return o;}
$.each(kmd.split(',').reverse(),function(i,v){komand = pusher(v,komand)});
$.extend(true,komands,komand);
}
console.log(komands)
这与jQuery无关。@Archer thx对于这个明智的答案,也许可以用简单的js将其存档,但这肯定是一个与jQuery相关的问题,因为im在js atm中显式工作。所以,下次也许就不要发表这样的评论,如果你控制初始数据的结构,你肯定会觉得使用对象而不是数组会更好。我还能像现在这样把它们相互推到一起吗?