Javascript 修改for循环
下面的代码可以使用for循环遍历数组,然后根据数组值将列名打印到excel工作表上,我想知道是否有可能修改代码,使其能够组合并分组包含单词“apples”的数组值在数组中的某个位置,只需创建一个excel列名“apples”,如下面提供的示例所示 我不确定如何修改for循环来实现这一点 预期的结果是:Javascript 修改for循环,javascript,arrays,excel,loops,Javascript,Arrays,Excel,Loops,下面的代码可以使用for循环遍历数组,然后根据数组值将列名打印到excel工作表上,我想知道是否有可能修改代码,使其能够组合并分组包含单词“apples”的数组值在数组中的某个位置,只需创建一个excel列名“apples”,如下面提供的示例所示 我不确定如何修改for循环来实现这一点 预期的结果是: 函数搜索\u数组(arr、str){ var searchExp=新的RegExp(str,“gi”); 返回(searchExp.test(arr))?真:假; } var temp=[“红
函数搜索\u数组(arr、str){
var searchExp=新的RegExp(str,“gi”);
返回(searchExp.test(arr))?真:假;
}
var temp=[“红苹果”、“富士苹果”、“格兰尼史密斯苹果”、“麦金托什苹果”、“蜜脆苹果”、“嘎拉苹果”、“橙子”、“梨”、“香蕉”]
var col=2
对于(变量i=0;i
在循环内的代码中添加一个标志:
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++;
}
}
var col=2,
foundApples=false,
应用程序安装;
对于(变量i=0;i
您可以先过滤数组,添加要查找的单词,然后迭代
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]
}
var temp=[“红苹果”、“富士苹果”、“格兰尼史密斯苹果”、“麦金托什苹果”、“蜜脆苹果”、“嘎拉苹果”、“橙子”、“梨”、“香蕉”]
var-col=2;
var-word='apples';
var=false;
温度=温度过滤器(功能(项目){
变量i=项目索引(单词)=-1;
如果(i)has=true;
返回i;
});
如果(有)温度未移位(字);
对于(变量i=0;i
您可以为过滤后的值创建一个新数组,并将这些新值推送到COL中:
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;
}
var temp=[“红苹果”、“富士苹果”、“格兰尼史密斯苹果”、“麦金托什苹果”、“蜜脆苹果”、“嘎拉苹果”、“橙子”、“梨”、“香蕉”]
var temp_filtered=[];
对于(变量i=0;i
如果水果总是“名字空间水果”(富士苹果)
我会这样做的
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;
}
var-fructlist=[“红苹果”、“富士苹果”、“格兰尼史密斯苹果”、“麦金托什苹果”、“蜜酥苹果”、“嘎拉苹果”、“橙子”、“梨”、“香蕉”]
控制台日志(uniqueFruit(水果列表));
功能独特水果(arr){
var returnarray=[];
对于(var i=0;i
这里有一个小提琴你是只想让“苹果”出现一次,其余的只允许重复,还是只允许唯一的列标题?我认为你代码的最后一行应该是
++col
唯一的列标题就行了。
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;
}