Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript中的for循环创建列表的问题(Problem with create list using for loop in Javascript)

I'm trying to crate a code that will count number of images I have and then will create a list with the number of images (for example, if I have 5 images, I'll get this : [0,1,2,3,4].

(我正在尝试创建一个代码,该代码将计算我拥有的图像数量,然后创建一个包含图像数量的列表(例如,如果我有5张图像,我将得到以下信息:[0,1,2, 3,4]。)

My code runs and I think it creates a list that is empty:

(我的代码运行,我认为它创建了一个空列表:) 在此处输入图片说明

This is my code (I have tried to put only the relevant part):

(这是我的代码(我仅尝试放入相关部分):)

//First I filter my image collection according to the number of pixels each image has
//Filter according to number of pixels

var ndviWithCount = withNDVI.map(function(image){
  var countpixels = ee.Number(image.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  crs: 'EPSG:4326',
  scale: 30,
  }).get('NDVI'));

  return image.set('count', countpixels);
});

print(ndviWithCount, 'ndviWithCount');

//Here I count what is the maximum number of pixels that image has and then I create new collection with //only "big images"

var max = ndviWithCount.reduceColumns(ee.Reducer.max(),  ["count"]);
print(max.get('max'));
var max_pix=max.get('max');

//filter between a range
var filter = ndviWithCount.filter(ee.Filter.rangeContains(
          'count', max_pix, max_pix));
print(filter, 'filtered');
//Here I try to grab the number of images so I can create a list
var num_images=filter.size();

//creating the list of images

var listOfImages =(filter.toList(filter.size()));

//Here is the loop that diesn't work
//I have tried to determine i=0, and then that it will iterate untill i is equal to the number of images
//I try to say, i=0, so add 1 and the nadd it to my list.
for (i = 0; i < num_images; i++) {
  var listOfNumbers=[];
  i=i.add(1);
  listOfNumbers.push(i);

}

my end goal is to have list that contains nmbers from 0 or 1 to the number of images I have.

(我的最终目标是让列表包含从0或1到我拥有的图像数量的nmber。)

  ask by Reut translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for (i = 0; i < num_images; i++) {
  var listOfNumbers=[];
  i=i.add(1);
  listOfNumbers.push(i);
}

You are initiating the array inside the for loop which isn't what you intend.

(您正在初始化for循环内的数组,这不是您想要的。)

I'm also not sure what i = i.add(1) is supposed to be.

(我也不确定i = i.add(1)应该是什么。)

May be helpful to check out info on for loops .

(检查有关循环的信息可能会有所帮助。)

let listOfNumbers = [];
for (let i = 0; i < num_images; i++) {
      listOfNumbers.push(i);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...