I'm having issues in pushing and returning an array with the events titles. As far as i'm concerned, this should append the title of the events into the titulos array.
for (j=0;j<events.length;j++){
var titulos = []
var events =a.getEventsForDay(testingstartdate, {search: 'OOO'});
var eventstitle = events[j].getTitle();
Logger.log(eventstitle);
titulos.push(eventstitle);
};
The Logger.log in question here is returning correctly one row per title, so no sure why the final array is only pushing 1 single value to it.
Any ideas?
The Logger.log in question here is returning correctly one row per
title, so no sure why the final array is only pushing 1 single value
to it.
This is happening because you define your array in each step of the for loop. Moving the following statement:
var titulos = [];
before the start of the for loop will solve your problem.
Related
I want to create a function taking three random different words from an array
I followed React native filter array not working on string to filter the array. However, the filter is not working.
takeThreeWords=()=>{
for(i=0;i<3;i++){
rand =(max, min=0)=> Math.floor(Math.random()*max-min)
randomWord=()=> this.state.wordsBank[rand(this.state.wordsBank.length)]
let aRandomWord = randomWord()
this.setState(prevState=>({
wordsToUse:[...prevState.wordsToUse, aRandomWord],
wordsBank: prevState.wordsBank.filter(word=>word!== aRandomWord)
}))
The last line is to make sure that no words from wordsBank are taken twice. However, the function works just as if the last line does not exist. wordsToUse take three words, but sometimes they are the same...
Can you please point me out what I am missing ?
You are updating wordsBank via this.setState, but your loop keeps operating on the initial copy of wordsBank, which has not been filtered.
The cleanest fix is to not call this.setState multiple times in a loop.
let wordsBank = this.state.wordsBank;
let extractedWords = [];
while(extractedWords.length<3) {
let i = ~~(Math.random()*wordsBank.length);
extractedWords.push(wordsBank.splice(i, 1)[0]);
}
this.setState(prevState=>({
wordsToUse: [...prevState.wordsToUse, ...extractedWords],
wordsBank
}));
The API I am accessing has an array (tickets), that contains a nested array of strings (tags). When I loop through to get to the nested array values, I am able to see the full list of tags (in the red box of the screenshot below).
When I view the sheet, however, it is only returning the first item of the array as you can see in the below screenshot.
I am sure this is something silly, but I cannot figure it out. My code is below. Thank you for any help you can provide.
var example = []
results.forEach(function(tickets){
var temp = [];
tickets.tags.forEach(function(tags){
temp.push(tags);
})
example.push([tickets["resolution_time"],tickets["created_at"], tickets["priority"], tickets["state"],tickets["id"], tickets["closed_by"], temp])
})
Logger.log(example + "is this working?");
var len = example.length;
//clear existing data
sheet.getRange(2,1,2000,8).clearContent();
//paste in the values
sheet.getRange(sheet.getLastRow() +1,1,len,7).setValues(example);
I see you trying to put an array of strings (temp) into a single cell of the sheet. Can you even do that?
Replace
var temp = [];
tickets.tags.forEach(function(tags){
temp.push(tags);
})
with
var temp = tickets.tags.join(',')
Simple question I hope (not sure what I am missing);
How can I push items to a basic and not an indexed array?
When doing this:
var shops = [];
angular.forEach(data, function(value, index) { //loop over data
shops.push(value.SHOP_CODE);
});
When you are logging the shops array to console, the array is probably empty and hence it says Array[0].
The contents of the array are evaluated again when you click the dropdown arrow beside Array[0]. So at the time of your click on the dropdown the array is not empty and hence showing 4 elements.
If the array were not empty at the point where you do console.log(shops), then you would get the result you are looking for.
I suggest you add a breakpoint at the statement console.log(shops) and check the contents of the array.
Thanks to #Patrick Evans and #SLePort, I realized I needed to wait for the promise. This works:
.$promise.then(function(data) {
$scope.workOrder = data;
angular.forEach(data, function(value, index) { //loop over aircraft data
shops.push(value.SHOP_CODE);
});
shops.sort();
console.log(shops);
});
My problem is pretty simple.
I'm trying to update a list of objects (localDatz) with another list of objects received after an AJAX request (data).
So It consists in two loops.. But when I try to update two objects, only one object is updated. There is something I really don't understand.
Any help ?
// fetch the localdata first
var localData = getAll();
// Loop through my 'localData'
$.each(localData.features, function(index,feature){
// Loop through the new data that are received
$.each(data.features, function(){
newFeature = this;
if (feature.properties.id==newFeature.properties.id){
// i think here is the problem..but can't figure out how to fix it
// I remove the old feature and push the new one
localData.features.splice(index,1);
localData.features.push(newFeature);
}
});
});
You are modyfing the list which you loop over with this code:
if (feature.properties.id==newFeature.properties.id){
localData.features.splice(index,1);
localData.features.push(newFeature);
}
and not only modyfing the list entries, but the order as well (you push to the end of the list), which messes up .forEach loop. Use simply:
if (feature.properties.id==newFeature.properties.id){
localData.features[ index ] = newFeature;
}
There is no need for using .splice at all.
It might be a silly question but still i am facing problem with this.
var eformDetailIds = [];
eformDetailIds=$("[name=eform_id]").map(function(){ return $(this).val() }).get();
this is the code that i have written in js function and calling this function on button click.
But the problem is the list eformDetailIds containing the previous values also. could you please how to set this empty list for every function call? Thanks in advance.
Just set the length to zero:
eformDetailIds.length = 0;
Or allocate a new array:
eformDetailIds = [];
Now, that said, according to the code you posted the entire array will definitely be replaced each time that ".map()" call runs. In other words, the previous values will not remain in the array. Perhaps you should post more to explain what it is that makes you think the old values remain.
Don't forget you can always reset the array in this way:
myArray = new Array();
It is pretty easy.