The use of for-loops is discouraged in Google Earth Engine because there is pointed out that the same results can be achieved using a map() operation with a specific function independently applied to each element. In following code, I have an initial list of zero values where I want to introduce ones in positions established by a list of indices of non nulls values (idxList). I spent a lot of time trying to find out a map operation for producing that list but, instead a list, it produces a matrix with values placed in adequate positions but it requires additional processing for getting a single list. However, the "discouraged" for-loop produces my desired solution by using directly 'set' method. Functional code looks as follows:
var option = ee.List.repeat(0, 8);
print("initial list", option);
var option2 = option;
var idxList = ee.List([1, 3, 4, 5, 6 ]);
print("index non nulls values", idxList);
var n = idxList.size().getInfo();
for (var i = 0; i < n; i += 1) {
option = option.set(idxList.get(i), 1);
}
print("desired solution but with for loop", option);
var new_list = ee.List([0, 1, 2, 3, 4]);
var int_lists = new_list.map(function setting (ele){
return option2.set(idxList.get(ele), 1);
});
print("result mapping a list", int_lists);
Is it possible to map a list with 'set' method (or using its 'iterate' method) for getting directly a desired result list, instead a matrix, avoiding for-loop use?