修改for循环

时间:2016-01-19 20:57:33

标签: javascript arrays excel loops

虽然下面的代码非常适合使用for循环遍历数组,然后根据数组值将列名打印到excel表上,但我想知道是否可以修改代码以使其成为可能将在数组中的某个地方组合并组合单词“apples”的数组值,并将一个excel列名称设为“apples”,如下面提供的示例所示。

我不确定如何修改for循环来完成此任务。

enter image description here

期望的结果是:

enter image description here

function search_array(arr, str){
  var searchExp = new RegExp(str,"gi");
  return (searchExp.test(arr))?true:false;
}

var temp = ["red apples", "fuji apples", "grannysmith apples", "mcintosh apples", "honeycrisp apples", "gala apples", "oranges", "pears", "bananas"] 

var col = 2
for(var i = 0; i < temp.length; i++){

    if (search_array(temp[i], "apples") == true) {

        Sheet.Cells(2,col).Value = "apples"

    }
    else {
        Sheet.Cells(2,col).Value = temp[i]
    }


++col
}

4 个答案:

答案 0 :(得分:1)

您可以先过滤数组,添加您要查找的单词,然后迭代

var temp = ["red apples", "fuji apples", "grannysmith apples", "mcintosh apples", "honeycrisp apples", "gala apples", "oranges", "pears", "bananas"] 
var col  = 2;
var word = 'apples';
var has  = false;

temp = temp.filter(function(item) {
    var i = item.indexOf(word) === -1;
    if (i) has = true;
    return i;
});

if (has) temp.unshift(word);

for(var i = 0; i < temp.length; i++){
    Sheet.Cells(2,col).Value = temp[i]
}

FIDDLE

答案 1 :(得分:0)

在循环内的代码中添加一个标志:

var col = 2,
    foundApples = false,
    hasApplesInString;

for(var i = 0; i < temp.length; i++){
    hasApplesInString = search_array(temp[i], "apples");

    if ( !hasApplesInString || !foundApples ) {
        Sheet.Cells(2,col).Value = temp[i];
        if (hasApplesInString) { foundApples = true };
        col++;
    }       
}

答案 2 :(得分:0)

您可以为过滤的值创建一个新数组,并将这些新值推送到cols:

var temp = ["red apples", "fuji apples", "grannysmith apples", "mcintosh apples", "honeycrisp apples", "gala apples", "oranges", "pears", "bananas"]

var temp_filtered = [];


for (var i = 0; i < temp.length; i++) {

    var value = temp[i]

    if (search_array(temp[i], "apples") == true) {

        value = "apples"

    }

    if (temp_filtered.indexOf(value) === -1) {
        temp_filtered.push(value);
    }

}

var col = 2;

for (var j = 0; j < temp_filtered.lenth; j++) {
    Sheet.Cells(2, col).Value = temp_filtered[j];
    ++col;
}

答案 3 :(得分:0)

如果水果总是成为“名称空间水果”(富士苹果)。

我就是这样做的。

var fruitList = ["red apples", "fuji apples", "grannysmith apples", "mcintosh apples", "honeycrisp apples", "gala apples", "oranges", "pears", "bananas"]

console.log(uniqueFruit(fruitList));

function uniqueFruit(arr) {
  var returnarray = [];
  for (var i = 0; i < arr.length - 1; i++) {//iterate through the fruitList 
    var fruit = arr[i].split(' ').reverse();//split each into an array of two words 
                                           // ["red","apples"]
                                           // Reverse the order
                                           // ["apples","red"]
    fruit = fruit[0];                      // fruit = "apples"
    if (returnarray.indexOf(fruit) < 0) {  // find apples in returnarray
      returnarray.push(fruit);             // if not there push it into it
    }
  }
  return returnarray;
}

这是一个小提琴https://jsfiddle.net/alex_at_pew/7o7tfrq8/

相关问题