BobbyJones
BobbyJones

Reputation: 1354

Modifyng a for loop

While the code below works great to iterate over an array using a for loop and then printing out column names based on the array values onto an excel sheet, I wanted to know if it was possible to have the code amended such that it would combine and group the array values that have the word "apples" somewhere in the array and just make one excel column name "apples" like the example provided below.

I am unsure as to how to modify the for loop to accomplish this.

enter image description here

The desired result is:

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
}

Upvotes: 1

Views: 63

Answers (4)

PEWColina
PEWColina

Reputation: 2124

if the fruit are always going to be "name space fruit" (fuji apples).

This is how I would do it.

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

Here is a fiddle https://jsfiddle.net/alex_at_pew/7o7tfrq8/

Upvotes: 0

Nikolay Ermakov
Nikolay Ermakov

Reputation: 5061

Add a flag to your code inside the loop:

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

Upvotes: 0

Adam
Adam

Reputation: 5233

You can create a new array for the filtered values and push these new values into the 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;
}

Upvotes: 0

adeneo
adeneo

Reputation: 318172

You could just filter the array first, add the word you're looking for, and then iterate

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

Upvotes: 1

Related Questions