简体   繁体   English

数组拆分为 2 和 map 对应的对象并将它们放入结果数组中

[英]Array split into 2 and map the corresponding objects and put them in the resultant array

I have an array of array of objects similar to this:我有一个类似于此的对象数组:

arr = [{
    val: 1,
    ts: 123
}, {
    val: 2,
    ts: 125
}, {
    val: 3,
    ts: 120
}, {
    val: 4,
    ts: 113
}, {
    val: 5,
    ts: 117
}, {
    val1: 6,
    ts: 143
}, {
    val1: 7,
    ts: 193
}, {
    val1: 8,
    ts: 187
}, {
    val1: 9,
    ts: 115
}, {
    val1: 10,
    ts: 116
}]

The length of the array is always an even number.数组的长度总是偶数。 Now basically I'd like to split them in 2 halves现在基本上我想把它们分成两半

split1 = [{ val:1},  {val :2}, ......{val:5}]
split2 = [{val1:6},{val1:7},.......{val1:10}]

Now I have to map over these arrays and combine their fields(first item of array1 with first item of array2 and so on) into one single object and add an extra id to it such that result will look like现在我必须对这些 arrays 进行 map 并将它们的字段(array1 的第一项与 array2 的第一项等)组合成一个 object 并添加一个额外的 id

final = [{val:1, val1:6 , id:1} , {val:2, val1:7,id:3} , {val:3, val1:8,id:3},{val:4, val1:9,id:4},{val: 5, val1:10, id:5}]

This should hold for all arrays of even length and I want to make it a dynamic one.这应该适用于所有均匀长度的 arrays,我想让它成为一个动态的。 Keys will never be repeated inside source array键永远不会在源数组中重复

What I have tried is:我试过的是:

var res= a.splice(0, arr.length/2);
var c = a.map((val, ind) => { {val, res[ind]} })

Can someone guide here?有人可以在这里指导吗?

You can try this solution你可以试试这个解决方案

 const arr = [{ val: 1},{val: 2},{val: 3}, {val: 4}, {val:5},{ val1: 6},{val1: 7},{val1: 8}, {val1: 9}, {val1:10}] const split = arr => [arr.slice(0, arr.length/2), arr.slice(arr.length/2, arr.length)] const splitedArr = split(arr); const merge = arr => { const merged = [] for(let i = 0; i < arr[0].length; i++){ let newObj = {...arr[0][i], ...arr[1][i], id:(i+1)} merged.push(newObj); } return merged; } const mergedArr = merge(splitedArr); console.log(mergedArr);

I have tried我努力了

var res= a.splice(0, arr.length/2); var c = a.map((val, ind) => { {val, res[ind]} })

You're quite close.你很接近。 To make the map callback syntactically valid and create the desired object, it should be为了使map回调在语法上有效并创建所需的 object,它应该是

var res = a.splice(0, arr.length/2);
var c = a.map((val, ind) => {
    return {val: val.val, val1: res[ind].val1, id: ind+1};
})

or或者

var c = a.map((val, ind) => {
    return Object.assign({id: ind+1}, val, res[ind]);
})

You're quite close, some minor fixes你很接近,一些小修复

  1. wrap map function implicit return with () instead of {}()而不是{}包装 map function 隐式返回
  2. for adding computed value you need to assign key name explicitly要添加计算值,您需要显式分配键名
  3. need to destructure val or need to access val.val需要解构 val 或者需要访问val.val

 const arr = [{ val: 1}, {val: 2}, {val: 3}, {val: 4}, {val: 5}, {val1: 6}, {val1: 7}, {val1: 8}, {val1: 9}, {val1: 10}]; const firstHalf = arr.slice(0, arr.length / 2); const secondHalf = arr.slice(arr.length / 2); const final = secondHalf.map((val, ind) => ({...val, ...firstHalf[ind], id: ind, })) console.log(final);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM