数组未正确建立索引

时间:2019-04-13 19:41:56

标签: javascript arrays indexing key

request_people最初是'254,,one,one,one,99,,one,one,ORCHARD,one,ON,one/255,254,TWO,TWO,TWO,99',它转换为:

request_people
    array(2): {
       [0]:  array(13): {
          [0]:  string(3): "254"
          [1]:  string(0): ""
          [2]:  string(3): "one"
          [3]:  string(3): "one"
          [4]:  string(3): "one"
          [5]:  string(2): "99"
          [6]:  string(0): ""
          [7]:  string(3): "one"
          [8]:  string(3): "one"
          [9]:  string(7): "ORCHARD"
          [10]:  string(3): "one"
          [11]:  string(2): "ON"
          [12]:  string(3): "one"
       }
       [1]:  array(6): {
          [0]:  string(3): "255"
          [1]:  string(3): "254"
          [2]:  string(3): "TWO"
          [3]:  string(3): "TWO"
          [4]:  string(3): "TWO"
          [5]:  string(2): "99"
       }
    }

我想通过将索引的第一个元素转换为实际索引来重新定义数组。

因此输出应为:

new_request_people
    array(2): {
       [254]:  array(12): {
          [0]:  string(0): ""
          [1]:  string(3): "one"
          [2]:  string(3): "one"
          [3]:  string(3): "one"
          [4]:  string(2): "99"
          [5]:  string(0): ""
          [6]:  string(3): "one"
          [7]:  string(3): "one"
          [8]:  string(7): "ORCHARD"
          [9]:  string(3): "one"
          [10]:  string(2): "ON"
          [11]:  string(3): "one"
       }
       [255]:  array(5): {
          [0]:  string(3): "254"
          [1]:  string(3): "TWO"
          [2]:  string(3): "TWO"
          [3]:  string(3): "TWO"
          [4]:  string(2): "99"
       }
    }

我有以下代码:

request_people = request_people.split('/');
for (i = 0; i < request_people.length; i++)
{
    request_people[i] = request_people[i].split(',');
}
//dump(request_people, 'alert');
var new_request_people = new Array();
for (i = 0; i < request_people.length; i++)
{

    var key = parseInt(request_people[i][0]);
    new_request_people[key] = request_people[i];
    dump(new_request_people, 'alert');
}

请注意,它似乎使数组具有给定值(69)的大小,并在最后添加“ test”。

1 个答案:

答案 0 :(得分:2)

您可以构建一个新数组,并将第一个值作为其余内部数组的索引。

结果是一个包含两个元素的稀疏数组。

.as-console-wrapper { max-height: 100% !important; top: 0; }
Sub FormatData()

Workbooks.Open FileName:="C:\Users\john.doe\Documents\scripts\apps\allow\Weekly_Cash_Trending"

Workbooks("Weekly_Cash_Trending.xlsx").Activate


Columns("C:C").Select
Selection.NumberFormat = "$#,##0"
Range("A1").Select

ActiveWorkbook.SaveAs FileName:="C:\Users\john.doe\Documents\scripts\apps\allow\Weekly_Cash_Trending"
End Sub