【问题标题】:Best way to make an array of associated values, based on multidimensional array基于多维数组制作关联值数组的最佳方法
【发布时间】:2013-11-07 11:00:16
【问题描述】:

我需要的是,当我单击一个单元格时,例如包含“PMS03”的单元格 [2,2],我想要一个显示所有包含 PMS03 的单元格的警报,这将是 alert("Positions : [2,1], [2,2], [3,7]");

我想在创建“pointmapS”(=puntmapS)的过程中我会以某种方式制作第二个数组,但我认为最好的办法是让一些像你们这样的专家帮助我,因为应该有很多我可能不知道的解决方案(并且更有效)。

创建映射数组:

var jPunten = JSON.parse(data); 
var puntmapS = []; 
var cardsS = []; 
var prevCard = jPunten[0].KARTNR; //previous card starts with the first card
cardsS[0] = jPunten[0].KARTNR;
//Generate point arrays
for (var i=0;i<jPunten.length;i++)
{
    if(prevCard!=jPunten[i].KARTNR){
        // (fill in cardsS, saves kartnr of the rows) gap-safety for if there should be gaps in the KARTNR's, currently only used for scounter
        cardsS[sCounter+1]=jPunten[i].KARTNR;sCounter++;
    } 
    //create an array under key jPunten[i].KARTNR, if it doesn't exist already
    puntmapS[sCounter] = puntmapS[sCounter] || []; // to avoid gaps we use sCounter instead of jPunten[i].KARTNR
    //then, assign your value
    console.log("S|| prevCard: "+ prevCard + " === " + jPunten[i].KARTNR +" (Kartnr)");

    puntmapS[sCounter][jPunten[i].BITNRK-1] = jPunten[i].STATDEV; //add point to row, -1 because array starts at 0

    console.log("S|| "+"  --SCounter: "+ sCounter + " bit: " + jPunten[i].BITNRK + "    = " + jPunten[i].STATDEV);
}

创建表:

if(puntmapS.length!=0){
    table = "<table style='margin-left:20px;font-size:80%;' border='1'><tbody><tr><th></th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th><th>11</th><th>12</th><th>13</th><th>14</th><th>15</th><th>16</th></tr><tr><th>"+cardsS[0]+ " S</th>";
    for (var i=0;i<sCounter+1;i++)
    {
        for (var x=0;x<16;x++)
        {
            if(puntmapS[i][x]!=undefined)
            {
                table+= "<td><a href='#' onClick='alert(\""+puntmapS[i][x]+"\")'>"+puntmapS[i][x]+"</a></td>";
            }
            else
            {
                table += "<td style='background-color:#E6E6E6'><a href='#' onClick='alert(\"EMPTY\")'>&nbsp;</a></td>";
            }
        }
        if(i<(sCounter)){
            table+="</tr><tr><th>"+(cardsS[i+1])+" S</th>";
        }
    }
    table += "</tr></table>";
}

jPunten 看起来像这样:

0
    Object { 0="LSZ09 ", 1="1", 2="S", more...} 
0
    "LSZ09 "    
1
    "1" 
2
    "S" 
3
    "0" 
4
    "1" 
5
    "0" 
6
    "I "    
BITNRK
    "1" 
BITSTATUS
    "0" 
DEVPKT
    "1"
KARTNR
    "0"
PKTTYP
    "S"
STATDEV
    "LSZ09 "    
TYPE
    "I "

【问题讨论】:

  • 我们很高兴您发布了生成表格的代码,但是显示您尝试完成的代码在哪里?你本质上只是要求人们为你工作。
  • 嗯...如果我理解正确,您不能只制作一个 HashMap,其中键是“PMS03”,值是位置列表吗?
  • 如果代码是为我制作的,那就太好了,但这不是我想要的。如果我有一些想法或更确切地说是一条可以遵循的途径,我将非常感激,因为我对 JavaScript 相当陌生。知道解决这个特定问题的最佳方法对我很有帮助。
  • @W.K.S 我不认为 JavaScript 中有类似 HashMap 的东西
  • 它们被称为关联数组。很简单:map['PMS03']=[{x:2,y:1},{x:2,y:2},{x:3,y:7}];

标签: javascript arrays multidimensional-array


【解决方案1】:

我的主要建议是使用underscoreJS,因为它有很多有用的帮助函数来操作数组,为您节省大量输入。作为您尝试解决的特定问题的解决方案,您可以使用以下内容:

var arr = [['a', undefined], ['b', 'a']] //2x2 2D test array
var result = _.chain(arr) //Create chainable underscore object
    .map(function(row, j){ //map cell values to objects containing both the value and position in the array
        return _.map(row, function(cell, i){
            return { pos: [i,j], value: cell };
        })
    })
    .flatten() //Put all cells in a single array
    .filter(function(cell){ //Filter the cells based on some criterium
        return cell.value === 'a';
    })
    .map(function(cell){ //Return the position rather than the object we generated
        return cell.pos;
    })
    .value(); //Return the original array instead of the chainable underscore object

【讨论】:

    【解决方案2】:

    您可以创建一个关联数组,其中键是“PMSO3”,值是包含位置数组的对象。

    map = [];
    map['PMSO3'] = {
        label: 'PMS03-100',
        id: 124,
        positions: [{
            x: 2,
            y: 1
        }, {
            x: 2,
            y: 2
        }, {
            x: 3,
            y: 7
        }]
    };
    
    console.log('PMSO3 is located at these positions\n');
    map['PMSO3'].positions.forEach(function(aPosition){
        console.log(aPosition.x+' '+aPosition.y+',');
    });
    

    JSFiddle

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2019-01-30
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2021-06-16
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多