简体   繁体   English

多维数组 JS 代码片段

[英]Multidimensional array JS snippest

In below given JavaScript code I am unable achieve expected output. please help me to get resolve the given code.在下面给出的 JavaScript 代码中,我无法实现预期的 output。请帮助我解决给定的代码。 Please check for the expected output.请检查预期的 output。

In below given JavaScript code I am unable achieve expected output. please help me to get resolve the given code.在下面给出的 JavaScript 代码中,我无法实现预期的 output。请帮助我解决给定的代码。 Please check for the expected output.请检查预期的 output。


    $(document).ready(function(){
        var record = []; 
        var idarr = ['5','2','-','3','-'];  
        var jobidarr = [];      
        var userid = 32;
        var newlogtimedata = ["2020/11/14 13:29:30","-","2020/10/10 13:33:49","-"];
        var newlogtimedataupdate = [];  
        var logcnt = 1;
        var j=0;
        
        for(var i = 0; i < newlogtimedata.length; i++){
                if(newlogtimedata[i] != "-"){
                    newlogtimedataupdate.push(newlogtimedata[i]);
            }
        }
        
        for(var i = 0; i < idarr.length; i++){
            if(idarr[i] == "-"){
                logcnt++;
            }
            else{
                //for(var j = 0; j < idarr.length; j++){
                    record[[j]] = new Array();
                    record[[j]].push(parseInt(idarr[i]));
                    record[[++j]]= new Array();
                    /* record[[j]].push(JSON.stringify(parseInt(userid)));
                    record[[j]].push("-");
                    record[[++j]] = new Array();
                    record[[j]].push(newlogtimedataupdate[logcnt-1]);
                    record[[j]].push("-"); */
                    j++;
                //}
            }
          }
          console.log("record:::", record);
    }); 

/*
expected output will be
record:::
[ [5, 32, ['2020/11/14 13:29:30','-'],          
      [2, 32, ['2020/11/14 13:29:30','-'],
      [3, 32, ['2020/10/10 13:33:49','-'] ];  */
      
      

I believe this your desired result, not sure what it's supposed to represent though:我相信这是您想要的结果,但不确定它应该代表什么:

 var idarr = ['5', '2', '-', '3', '-']; var userid = 32; var newlogtimedata = [ "2020/11/14 13:29:30", "-", "2020/10/10 13:33:49", "-" ]; const result = idarr.reduce((a, c, i) => { if (c === '-') return a; a.push([ parseInt(c, 10), userid, [ newlogtimedata[i] === '-'? newlogtimedata[i - 1]: newlogtimedata[i], '-' ] ]); return a; }, []); console.log(result)

Try this:试试这个:

for(var j = 0; j < idarr.length; j++){
    record.push([
        parseInt(idarr[i]),
        userid,
        [newlogtimedataupdate[logcnt - 1], '-']
    ]);
 }

To access data inside multi-dimensional arrays, it looks like this myArray[2][1] .要访问多维 arrays 中的数据,它看起来像这样myArray[2][1]

Example: Here's a multi-dimensional array with some random values:示例:这是一个包含一些随机值的多维数组:

let myArray = [
    ['cake', 'cookie'],
    ['car', 'plane', 'train']
];

To access the elements inside myArray , you first select the index of one of the inner arrays. That means myArray[0] looks like: ['cake', 'cookie'] , and now you can select the elements inside the inner array like so: myArray[0][1] (which is 'cookie' ).要访问myArray中的元素,您首先需要 select 内部 arrays 之一的索引。这意味着myArray[0]看起来像: ['cake', 'cookie'] ,现在您可以 select 内部数组中的元素所以: myArray[0][1] (这是'cookie' )。

You could use two indices for the arrays and pick an item as long as no sparator is coming.您可以为 arrays 使用两个索引并选择一个项目,只要没有 sparator 出现即可。

 let idarr = ['5', '2', '-', '3', '-'], userid = 32, newlogtimedata = ["2020/11/14 13:29:30", "-", "2020/10/10 13:33:49", "-"], SEPARATOR = '-', record = [], i = 0, j = 0; while (i < idarr.length && j < newlogtimedata.length) { record.push([idarr[i], userid, [newlogtimedata[j], '-']]); if (idarr[i + 1] === SEPARATOR && newlogtimedata[j + 1] === SEPARATOR) { i += 2; j += 2; continue; } if (idarr[i + 1];== SEPARATOR) i++; if (newlogtimedata[j + 1].== SEPARATOR) j++; } console.log(record);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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