[英]Arrays withing an array: how to push all elements forward by one with javascript
This is my array of arrays: 这是我的数组数组:
arr_1 = [1,2,3]
arr_2 = [4,5,6]
arr_3 = [7,8,9]
arr = [arr_1, arr_2, arr_3]
arr = [[1,2,3], [4,5,6], [7,8,9]]
What I want to do is push all elements like so that the final array is like the following and insert another element at the beginning of my array: 我想要做的是推送所有元素,以便最终数组如下所示,并在我的数组的开头插入另一个元素:
arr = [[i,1,2], [3,4,5], [6,7,8], [9]]
All sub-arrays must not be more than 3 elements. 所有子阵列不得超过3个元素。
Thanks for your help. 谢谢你的帮助。
You could visit all inner arrays and unshift the leftover values from the previous loop. 您可以访问所有内部数组并从前一个循环中取消剩余的值。
var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], chunk = 3, item = 'x', i = 0, temp = [item]; while (i < array.length) { array[i].unshift(...temp); temp = array[i].splice(chunk, array[i].length - chunk); i++; } if (temp.length) { array.push(temp); } console.log(array.map(a => a.join(' ')));
You can use the function reduce
您可以使用函数
reduce
var arr = [[1,2,3], [4,5,6], [7,8,9]], newElem = "newOne", all = [newElem, ...arr.reduce((a, c) => [...a, ...c], [])], // All together // Build the desired output asking for the result of: // element mod 3 === 0 result = all.reduce((a, c, i) => { if (i % 3 === 0) a.push([c]); else a[a.length - 1].push(c); return a; }, []); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could move on each iteration last element from previous array to next one and if the last sub-array has more then 3 elements then remove the last one and add it to new array. 您可以将每个迭代的最后一个元素从前一个数组移动到下一个数组,如果最后一个子数组有超过3个元素,则删除最后一个元素并将其添加到新数组。
let arr_1 = [1, 2, 3], arr_2 = [4, 5, 6], arr_3 = [7, 8, 9], arr = [arr_1, arr_2, arr_3] setInterval(function() { const last = arr.length - 1; const newElement = parseInt(Math.random() * 30) arr.forEach((a, i) => { if(i == 0) a.unshift(newElement); if(arr[i + 1]) arr[i + 1].unshift(a.pop()) else if(arr[last].length > 3) arr[last + 1] = [arr[last].pop()] }) console.log(JSON.stringify(arr)) }, 1000)
You can do this quite succinctly with a simple unravel/ravel. 你可以用一个简单的解开/拉扯完全简洁地做到这一点。 It easy to adjust group size too.
它也很容易调整组大小。
let arr = [ [1,2,3], [4,5,6], [7,8,9]] let newEl = 0 let groupSize = 3 var newArr = []; var unravelled = arr.reduce((a, c) => a.concat(c), [newEl]) while(unravelled.length) newArr.push(unravelled.splice(0,groupSize)); console.log(newArr)
arr_1 = [1, 2, 3] arr_2 = [4, 5, 6] arr_3 = [7, 8, 9] arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] function reassignChunk(x) { // create an array which will be used to store unwrapped values var newArray = []; arr.forEach(function(elem) { newArray.push(...elem); //using spread operator to unwrap values }); newArray.unshift(x, limit) var modArray = []; var m, j, temparray, chunk = 3; for (m = 0; m < newArray.length; m = m + limit) { // creatinging new array using slice modArray.push(newArray.slice(m, m + limit)); } console.log(modArray) } reassignChunk(13, 3)
arr = [[i,1,2], [3,4,5], [6,7,8], [9]]
Assuming all your elements are numbers, you can do it like this: 假设你的所有元素都是数字,你可以这样做:
i
to the array i
到数组中 const arr_1 = [1,2,3]; const arr_2 = [4,5,6]; const arr_3 = [7,8,9]; const i = 42; const result = [i,...arr_1,...arr_2,...arr_3].join() .match(/(?:[^,]+(,|$)){1,2}[^,]*/g).map( x => x.split(',').map(Number) ) ; console.log( result );
You may do your 2d unshifting simply as follows; 您可以按照以下方式进行2d不移位;
var arr_1 = [1,2,3], arr_2 = [4,5,6], arr_3 = [7,8,9], arr = [arr_1, arr_2, arr_3], us2d = (a2d,...is) => is.concat(...a2d) .reduce((r,e,i) => (i%3 ? r[r.length-1].push(e) : r.push([e]), r), []); console.log(JSON.stringify(us2d(arr,0))); console.log(JSON.stringify(us2d(arr,-2,-1,0)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.