简体   繁体   English

带有数组的数组:如何使用javascript将所有元素向前推送一个

[英]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: 假设你的所有元素都是数字,你可以这样做:

  1. Prepend i to the array i到数组中
  2. Flatten the array 展平阵列
  3. Convert the array to a comma-separated string 将数组转换为逗号分隔的字符串
  4. Split the string into chunks of at most 3 numeric substrings (2 commas) 将字符串拆分为最多3个数字子串(2个逗号)的块
  5. Convert the chunks back into arrays of numbers 将块转换回数组

 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.

相关问题 javascript同时将两个数组的元素推入一个数组 - javascript simultaneously push elements of two arrays into one array 如何设置一个 arrays 的所有元素必须至少等于 JavaScript 中另一个数组的一个元素的条件? - How to set up consition that all elements of one arrays must be equal to at least one element of the another array in JavaScript? 如何将类中的所有对象都推送到一个数组javascript中? - How to push all objects in class into one array javascript? 如何在javascript中的新数组中使用相同的appName从两个不同的数组中推送元素 - how to make push elements from two different arrays with same appName inside a new Array in javascript Typescript/Javascript 数组推送到二维数组中的所有 arrays - Typescript/Javascript array push pushes to all arrays in 2d array 如何通过循环函数将N个数组的所有数组变量推入一个数组中? - How can I push all array variables of N arrays in just one array over an loop function? 如何识别一个数组的所有元素是否存在于另一个数组中 - Javascript How to identify if all elements of one array are present in another JavaScript将输入推入Array会导致多个数组而不是一个 - Javascript push input into Array causes several arrays not one 如何从javascript数组推送JSON中的元素 - How to push elements in JSON from javascript array 如何将元素推入3D数组(JavaScript) - How to push elements to a 3D array (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM