合并 2 个数组,同时忽略空字段

我试图将数组2合并到数组1中,以便空字段保持像数组1一样,并且具有值的字段在数组1处从数组2被覆盖。但只有空字段,如果它是空的,则应将其视为数字并覆盖字段。请看这两条评论,以便更好地理解我的意思。


我尝试过:


const array1 = [[2], [], [3,5], [], [1]]

const array2 = [[], [], [4], [], [null]]


Array.prototype.splice.apply(array1, [0, array2.length].concat(array2))

console.log(array2)


// this logs [[], [], [4], [], [null]]


// what is should log is [[2], [], [4,5], [], [null]]


扬帆大鱼
浏览 137回答 4
4回答

慕桂英4014372

通过覆盖合并编写泛型函数的好处是巨大的。我正在使用在另一篇文章中写的,不需要修改来支持您当前的需求 -merge// main.jsimport { merge } from './util'const array1 = [[2], [], [3,5], [], [1]]const array2 = [[], [], [4], [], [null]]const result =&nbsp; merge(array1, array2)console.log(JSON.stringify(result))// [[2],[],[4,5],[],[null]]merge很有帮助,因为它可以直观地处理任何可以想象到的形状的嵌套对象和数组(甚至是稀疏数组!) -// util.jsconst isObject = x =>&nbsp; Object(x) === xconst mut = (o = {}, [ k, v ]) =>&nbsp; (o[k] = v, o)const merge = (left = {}, right = {}) =>&nbsp; Object&nbsp; &nbsp; .entries(right)&nbsp; &nbsp; .map&nbsp; &nbsp; &nbsp; ( ([ k, v ]) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isObject(v) && isObject(left[k])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? [ k, merge(left[k], v) ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : [ k, v ]&nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; .reduce(mut, left)export { merge }展开下面的代码段,以在您自己的浏览器中验证结果 -// util.jsconst isObject = x =>&nbsp; Object (x) === xconst mut = (o = {}, [ k, v ]) =>&nbsp; (o[k] = v, o)const merge = (left = {}, right = {}) =>&nbsp; Object&nbsp; &nbsp; .entries(right)&nbsp; &nbsp; .map&nbsp; &nbsp; &nbsp; ( ([ k, v ]) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isObject(v) && isObject(left[k])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? [ k, merge (left[k], v) ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : [ k, v ]&nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; .reduce(mut, left)// export { merge }// main.js// impor { merge } from './util'const array1 = [[2], [], [3,5], [], [1]]const array2 = [[], [], [4], [], [null]]const result =&nbsp; merge(array1, array2)console.log(JSON.stringify(result))// [[2],[],[4,5],[],[null]]不可变合并在本例中,我们的函数将永久更改其中一个输入数组。这是一个变体,它接受任意数量的对象/数组,并允许我们轻松创建新数组,而无需更改任何输入mergearray1mergeconst array1 = [[2], [], [3,5], [], [1]]const array2 = [[], [], [4], [], [null]]const result =&nbsp; merge([], array1, array2)console.log("result:", JSON.stringify(result)) // [[2],[],[4,5],[],[null]]console.log("array1:", JSON.stringify(array1)) // [[2],[],[3,5],[],[1]]console.log("array2:", JSON.stringify(array2)) // [[],[],[4],[],[null]]以下是修改后的模块可能的外观 -util// util.jsconst isArray =&nbsp; Array.isArrayconst isObject = x =>&nbsp; Object(x) === xconst merge2 = (l = null, r = null) => // <- private; not exported&nbsp; isArray(l) && isArray(r)&nbsp; &nbsp; ? merge([], l, r): isObject(l) && isObject(r)&nbsp; &nbsp; ? merge({}, l, r): rconst merge = (init = {}, ...all) =>&nbsp; // <- public interface&nbsp; all.reduce(replace, init)const replace = (r = {}, o = {}) =>{ for (const [ k, v ] of Object.entries(o))&nbsp; &nbsp; r[k] = merge2(r[k], v)&nbsp; return r}export { merge }这最初是为另一个问题写的,但从未发表过。我很高兴我有一个地方终于发布了它。享受!展开下面的代码段以验证浏览器中的结果 -//util.jsconst isArray =&nbsp; Array.isArrayconst isObject = x =>&nbsp; Object(x) === xconst merge2 = (l = null, r = null) =>&nbsp; isArray(l) && isArray(r)&nbsp; &nbsp; ? merge([], l, r): isObject(l) && isObject(r)&nbsp; &nbsp; ? merge({}, l, r): rconst merge = (init = {}, ...all) =>&nbsp; all.reduce(replace, init)const replace = (r = {}, o = {}) =>{ for (const [ k, v ] of Object.entries(o))&nbsp; &nbsp; r[k] = merge2(r[k], v)&nbsp; return r}// export { merge }// main.js// import { merge } from './util'const array1 = [[2], [], [3,5], [], [1]]const array2 = [[], [], [4], [], [null]]const result =&nbsp; merge([], array1, array2)console.log("result:", JSON.stringify(result))// [[2],[],[4,5],[],[null]]console.log("array1:", JSON.stringify(array1))// [[2],[],[3,5],[],[1]]console.log("array2:", JSON.stringify(array2))// [[],[],[4],[],[null]]

Qyouu

您需要一个函数来获取两个数组并合并它们:merge_arrays([3,5], [4]);//=> [4,5]merge_arrays([3,5], []);//=> [3,5]merge_arrays([3,5], [,7]);//=> [3,7]merge_arrays([3,5], [,7,8]);//=> [3,7,8]下面是一个可能的实现:const merge_arrays =&nbsp; ([xh, ...xt], [yh, ...yt], ret = []) =>&nbsp; &nbsp; &nbsp; xh === undefined && yh === undefined ? ret&nbsp; &nbsp; : yh === undefined&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;? merge_arrays(xt, yt, [...ret, xh])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: merge_arrays(xt, yt, [...ret, yh]);有了这个,你可以 - 假设 array1 和 array2 具有相同的长度 - 映射并应用于当前元素和位于同一索引处的元素:array1merge_arraysarray2const array1 = [[2], [], [3,5], [], [1]];const array2 = [[], [], [4], [], [null]];array1.map((arr, idx) => merge_arrays(arr, array2[idx]));//=> [[2], [], [4,5], [], [null]]

胡子哥哥

您可以映射两次:array1.map((arr,&nbsp;i)&nbsp;=>&nbsp;arr.map((v,&nbsp;j)&nbsp;=>&nbsp;j&nbsp;in&nbsp;array2[i]&nbsp;?&nbsp;array2[i][j]&nbsp;:&nbsp;v));

潇潇雨雨

老式的,但你可以看到整个过程:)编辑:添加了一行。const array1 = [[2], [], [3,5], [], [1]];const array2 = [[], [], [4], [], [null]];/*// verbose wayif (array1.length == array2.length) {&nbsp; for (var i=0; i < array1.length; i++) {&nbsp; &nbsp; if (array1[i].length > 0) {&nbsp; &nbsp; &nbsp; array1[i] = array2[i];&nbsp; &nbsp; }&nbsp; }}*/// concise waynewArr = array1.map((el, i) => el.length > 0 ? array2[i] : el);console.log(newArr);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript