Javascript:在for循环完成后将最终元素添加到数组

时间:2017-03-08 04:25:42

标签: javascript arrays for-loop

所需结果的说明

我的来源是超过30,000行非常结构化的文本,前面的数字递增,然后递增后面的数字并用冒号分隔。剥离非必需品,我留下了以下sourceArray,为方便起见被截断:)

sourceArray = ["001:001", "001:002", "001:003", 
               "002:001", "002:002", 
               "003:001", "003:002"]; 

我正在尝试计算每个前端号码的后退号码并将其推送到阵列。在伪代码中,我的最终结果应如下所示:

myArray[totalNumberOf_001_Items, totalNumberOf_002_Items, totalNumberOf_003_Items]

在我的简单示例中应该给出最终值:

[3, 2, 2]

问题和疑问

我的for循环在我的数据末尾结束,因此我的数组中有一个元素短。

如何制作"额外通行证"通过循环或是否有另一种方法将最终元素推送到我的数组?



var sourceArray = ["001:001", "001:002", "001:003",
  "002:001", "002:002",
  "003:001", "003:002"
];
var myArray = [];
var frontCounter = 1;
var backCounter = 1;

for (var i = 0; i < sourceArray.length; i++) {
  var text = sourceArray[i];
  var front = text.substr(0, 3);
  front = Number(front);
  var back = text.substr(4, 3);
  back = Number(back);

  if (front == frontCounter) {
    backCounter++;
  } else {
    myArray.push(backCounter - 1);
    backCounter = 2;
    frontCounter++;
  }
}
console.log(myArray); // result [3, 2]
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

这是您可以使用的替代方案,这样您就不必遍历整个(30,000)行。使用while循环,以便在到达0后立即中断;使用Map来存储唯一编号,方法是将索引/前面的数字设为key,并使其value成为一个对象,作为计数器来跟踪其总数。如果密钥存在,则更新总数;如果没有,则创建一个新的计数器对象。然后通过Map将其转换为只包含总数的数组,将map转换为所需的数组。

var sourceArray = ["001:001", "001:002", "001:003",
  "002:001", "002:002",
  "003:001", "003:002"
];

function getTotal(sourceArray) {

  let count = new Map();
  let update = item => count.get(item).total++;
  let create = item => count.set(item, {total: 1});

  const getItem = index => {
    let text = sourceArray[index];
    return text.substr(0, 3);
  }

  let index = -1;
  let start = 0;
  
  while (index != 0 && start < sourceArray.length) {
    index = getItem(start++);
    count.has(index) ? update(index) : create(index);
  }

  return [...count.values()].map(item => item.total);
}

console.log(getTotal(sourceArray));

答案 1 :(得分:0)

您可以使用下面的对象来跟踪文本片段出现的次数,文本是键,以及它们出现的次数值。从那里你可以建立你的阵列

var sourceArray = ["001:001", "001:002", "001:003", 
                   "002:001", "002:002", 
                   "003:001", "003:002"]; 

var frontEncounters = {};

function updateFrontEncounters(frontEncounters, front){
    var keys = Object.keys(frontEncounters);
    if(keys.indexOf(front) == -1)
    {
        frontEncounters[front] = 1;
    }
    else
    {
        frontEncounters[front] += 1;
    }
}

for(var item in sourceArray){
    var text = sourceArray[item];
    var front = text.substr(0, 3);
    var back = text.substr(4, 3);
    updateFrontEncounters(frontEncounters, front);  
}

var keys = Object.keys(frontEncounters);
var myArr = [];

for(var key in keys)
{
    myArr.push(frontEncounters[keys[key]])
}
console.log(myArr);

答案 2 :(得分:0)

使用对象存储&#34;前面&#34;数字和他们的数量。

for (var i = 0; i < sourceArray.length; i++) {
    var num = sourceArray[i].slice(0,3);
    counts[num] = counts[num] ? counts[num]+1 : 1;
}

完成后,您可以非常轻松地将其转换为数组:

var result = Object.keys(counts).map(function (key) {
    return counts[key];
});

使用ES-2017,它更容易:

var result = Object.values(counts)

工作代码段

&#13;
&#13;
var sourceArray = ["001:001", "001:002", "001:003", 
                   "002:001", "002:002", 
                   "003:001", "003:002"]; 
var counts = {};

for (var i = 0; i < sourceArray.length; i++) {
    var num = sourceArray[i].slice(0,3);
    counts[num] = counts[num] ? counts[num]+1 : 1;
}
console.log(counts);
var result = Object.keys(counts).map(function (key) {
    return counts[key];
});
console.log(result);

// ES-2017
//console.log(Object.values(counts));
&#13;
&#13;
&#13;