简体   繁体   English

如何动态构建一个javascript对象并在嵌套循环中推入数组?

[英]How to dynamically build a javascript Object and push into Array within nested loop?

I'm trying to dynamically build Objects and push them one by one into an Array.我正在尝试动态构建对象并将它们一个一个地推送到一个数组中。

My code so far is this:到目前为止,我的代码是这样的:

matrix([[0,1,1], [1,00], [1,1,1])

const matrix = (sequence) => {
   const rows = {}
   const rowsAry = []
   let row
   let idx
  
   for (let i=0; i < samples.length; i++) {
       row = `row${i}`
    
       for (let j=0; j < samples[i].length; j++) {
          if (samples[i][j] === 1) {
               idx = []
               rows[row] = rows[row] + 1 || 1 
               rows['indeces'] = idx.push(j)  
               rowsAry.push(rows)
            }
            
        }
    }
    console.log(rowsAry)

}

This obviously isn't working.这显然行不通。 I'm trying to map out all the '1's' in the sequence and know how many are in a row and what there indices are.我正在尝试绘制序列中的所有“1”,并知道一行中有多少个以及索引是什么。

The wrong out put is:错误的输出是:

[ { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 },
  { row0: 3, indeces: 1, row1: 2, row2: 2 } ]

Hoped for output would be:希望的输出是:

[{row1: 2, indices: [1,2]}, 
 {row2: 1, indices: [0]}, 
 {row3: 3, indices: [0,1,2]}
] 

You need to create a new object each time through the outer loop.您需要每次通过外循环创建一个新对象。 And only push it onto rowsAry once, not each time through the inner loop.并且只将它推送到rowsAry一次,而不是每次都通过内部循环。

 const matrix = (samples) => { const rowsAry = [] for (let i = 0; i < samples.length; i++) { let row = `row${i}` let indices = []; for (let j = 0; j < samples[i].length; j++) { if (samples[i][j] === 1) { indices.push(j); } } rowsAry.push({[row]: indices.length, indices: indices}); } console.log(rowsAry) } matrix([ [0, 1, 1], [1, 00], [1, 1, 1] ])

You can solve it by simply using array map and forEach method.您可以通过简单地使用数组映射forEach方法来解决它。 Traverse the array of arrays and check for ones to the inside array by using array forEach method and if found put it to the resultant array.遍历数组数组并使用数组 forEach 方法检查内部数组中的数组,如果找到,则将其放入结果数组。 At last, using the resultant array make your required object.最后,使用结果数组制作所需的对象。

 const matrix = (sequence) => { return sequence.map((x, i) => { const ret = []; x.forEach((y, j) => { if (y === 1) ret.push(j); }); const key = `row${i + 1}`; const obj = { [key]: ret.length, indices: ret }; return obj; }); }; const ret = matrix([ [0, 1, 1], [1, 0, 0], [1, 1, 1], ]); console.log(ret);

Or nested reduce() calls...或者嵌套的reduce()调用...

 const matrix = (sequence) => { return sequence.reduce((a, r, i) => { indices = r.reduce((ra, rn, ri) => { if (rn === 1) ra.push(ri); return ra; }, []); a.push({[`row${i}`]: indices.length, indices: indices}); return a; }, []); } console.log(matrix([[0,1,1], [1,0,0], [1,1,1]]))

But in reference to Barmar's comment about using different rowX keys a more accessible shape for your objects might look something along the lines of但是参考 Barmar 关于使用不同rowX键的评论,您的对象更易于访问的形状可能看起来类似于

{row: 1, count: 2, indices: [1, 2]}

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

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