1

在下面给定的 JavaScript 代码中,我无法实现预期的输出。请帮我解决给定的代码。请检查预期的输出。

在下面给定的 JavaScript 代码中,我无法实现预期的输出。请帮我解决给定的代码。请检查预期的输出。


    $(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','-'] ];  */
      
      
4

3 回答 3

0

我相信这是您想要的结果,但不确定它应该代表什么:

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)

于 2020-11-15T09:17:24.363 回答
0

试试这个:

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

要访问多维数组中的数据,它看起来像这样myArray[2][1]

示例:这是一个包含一些随机值的多维数组:

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

要访问 内部的元素myArray,首先选择其中一个内部数组的索引。这意味着myArray[0]看起来像:['cake', 'cookie'],现在您可以像这样选择内部数组中的元素:(myArray[0][1]'cookie')。

于 2020-11-15T09:19:52.097 回答
0

只要没有分隔符,您就可以为数组使用两个索引并选择一个项目。

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; }

于 2020-11-15T09:20:50.713 回答