gpt4 book ai didi

javascript - 根据对象名称合并嵌套的对象数组

转载 作者:行者123 更新时间:2023-12-01 02:12:57 25 4
gpt4 key购买 nike

所以我有像这样的数组数组:

array1 = [boot = [value = [ enable =["some string"]]]]

和一个对象数组:

array2 = [options=[ enable=[]], boot =[value =[something =[]]]]

我需要使用名称的“路径”将 array1 合并到 array2,以便得到:

output = [options=[ enable=[]], boot =[value =[something[], enable =["some string"]]]

所以我必须深入遍历对象,检测对象的通用名称,并将 array1 的其余部分推送到 array2 中的正确位置。知道该怎么做吗?我尝试了递归函数,但它没有给我预期的结果!这是我写的函数:

function mergeArrays(menu, array){
const arrayItem = Object.keys(array)[0];
var index = -1;
Object.values(menu).map(menuItem =>{
if(Object.keys(menuItem).indexOf(arrayItem) !== -1){
index = Object.keys(menuItem).indexOf(arrayItem);
}
})
if(index === -1){
menu.push(array);
return menu;
}else{
const menuItem = Object.values(menu)[index];
const rest = Object.values(menuItem);
const arrayItem = Object.values(array)[0];
mergeArrays(rest, arrayItem);
}
}

最佳答案

请注意a = [ v = 1 ]是一个数组,而不是一个对象。与 v = 1; a = [ v ] 相同,归结为 a = [ 1 ] ,如果你想读取第一个值,你必须写 a[0] 。但是,由于您的合并策略是“基于对象名称”,因此使用字典 d = { v : 1 } 似乎更合适。 。在这种情况下,您可以通过名称获取值 d["v"] .

在 JavaScript 世界中,人们使用“对象”这个词而不是“字典”。无论用什么词,您需要记住的是对象和数组是不同的,因为它们继承自不同的类。确实,{}意味着 new Object() ,而[]意味着 new Array() 。此外,大多数时候人们用 for ... in 枚举对象的属性。循环,并使用 for ... of 迭代数组的元素。循环或使用 for环形。再次,知道您对对象的名称感兴趣,for ... in循环可能是最适合您的选择。

话虽如此,尽管我们在评论中进行了讨论,但我仍然不太清楚您想要实现的目标。因此,我决定编写一个通用的合并函数。此函数能够合并任何类型的对象。1以下是一些用例:

> | merge("azerty", null) // b wins
< | null
> | merge("azerty", { t : true }) // b wins
< | { t : true }
> | merge({ t : true }, "azerty") // b wins
< | "azerty"
> | merge([1, 2, 3], [undefined, 5]) // a[0] wins, b[1] wins
< | [1, 5, 3]
> | merge("azerty", undefined) // a wins
< | "azerty"

合并策略非常简单,可概括如下:

If a is an array and b is an array, or if a is a dictionary and b is a dictionary, then merge them recursively, otherwise, b always wins unless it's undefined.

main({
m : [ 1, { ms : "ms", mf : false }, 3 ],
n : { ns : "ns", na : [ null, undefined ] },
o : { os : "os" }
}, {
m : [ -1, { mt : true } ],
n : { nt : true, na : [ undefined, null ] }
});

function merge (a, b) {
if (isFunction(b)) {
return b;
} else if (isArray(b)) {
if (!isArray(a)) {
return b;
} else {
var max = Math.max(
a.length, b.length
);
for (var i = 0; i < max; i++) {
b[i] = merge(a[i], b[i]);
}
return b;
}
} else if (isComplex(b)) {
if (!isComplex(a) || isArray(a) || isFunction(a)) {
return b;
} else {
for (var k in a) {
b[k] = merge(a[k], b[k]);
}
return b;
}
} else if (b === undefined) {
if (isFunction(a)) {
return a;
} else if (isArray(a)) {
return merge(a, []);
} else if (isComplex(a)) {
return merge(a, {});
} else {
return a;
}
} else {
return b;
}
}

function main (a, b) {
var pre = document.getElementsByTagName("pre");
b = merge(a, b);
pre[0].textContent = "a = " + (
fixUndef(JSON.stringify(a, markUndef, 2))
);
pre[1].textContent = "b = " + (
fixUndef(JSON.stringify(b, markUndef, 2))
);
}

function isComplex (x) {
return x === Object(x);
}

function isArray (x) {
return x instanceof Array;
}

function isFunction (x) {
return typeof x === "function";
}

// JSON.stringify() replaces
// `undefined` with `null`, the
// below functions are meant to
// fix this behavior.

function markUndef (k, v) {
return v === undefined ? "UNDEF" : v;
}

function fixUndef (s) {
return s.replace("\"UNDEF\"", "undefined");
}
pre {
display : inline-block;
vertical-align: top;
width: 50%;
}
<pre></pre><pre></pre>

<小时/>

1 在使用这个函数之前,我让你来证明算法的正确性:-P

关于javascript - 根据对象名称合并嵌套的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com