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);
});
Related
I'm working on a filter, which filters a array of nested arrays down to the value of one last index.
This happens in 5 steps. At each step you choose which index value (string) get used to filter the array further.
Example: You have 5 categories, each have 6 themes. Each of these 6 themes has 6 focusses(sub themes). Each focus has 6 questions. Each question has 1 answer. First you pick a categorie. This narrows the arrays down to all arrays with that categorie. Then a theme, which narrows the array down to only the arrays with that theme... etc...
So far I managed to filter down to the right question.
You can find the code here: https://github.com/okestens/vertical-filter.git
To get this work, I hardcoded the string "Deskundigheid" as a condition for the equality operator (===) that get used for the filter.
Example:
// FILTER QUESTIONS // I tried to use state.focus[0] but it does not work
let unique_questionsA = [. // now this is hardcoded
...new Set(formsA.filter((i) => i[2] === "Deskundigheid").map((i) => i[3])),
]; --------------
// FUNCTION
function displayQuestionsA() {
state.questions = [];
unique_questionsA.forEach(function (question, index) {
document.getElementById("question" + index).innerHTML = question;
state.questions.push(question);
});
------
// the state object
let state = {
category: [],
themes: [],
focus: [],
question: [],
answer: [],
};
But. What I want this filter to use is not a hardcoded string (deskundigheid) but the exact string that is visible in the div (coming from a click event which creates this filtered array and get stored in the state object). See image.
I thought: I need to track these arrays (with an object called 'state', capturing these dynamic arrays). If I then want to filter the right questions, by using the value (string) of the chosen focus (For example 'Deskundigheid', which is visible to the user), I just refer to the corresponding index value (state.focus[0]) of that chosen focus string, coming from the dynamic state object.
But, if I use the index state.focus[0] for the filter which creates the questions array, I get an empty array :(
My thought: Although the empty focus array (inside the state object), which get filled by a click event, eventually is filled with the right strings, the filter for the new array (unique_questionsA), which uses 'state.focus[0]' does not read the filled array as ‘filled’ but as empty.
I have not idea why :(
I hope I'm clear. If so, and you maybe have a clue, I would love to have a chat! Thanks O
The question can be summed up to
how do I get the text of the element when clicked, in an onclick event
listener callback function.
Your focusScript.js can be modified to
function displayQuestionsA(e) {
state.questions = [];
let unique_questionsA = [...new Set(formsA.filter((i) => i[2] === e.target.innerText).map((i) => i[3]))];
}
document.querySelector(".focus__A").addEventListener("click", displayQuestionsA);
Notice the e.target.innerText which contains the text inside the element that triggered the event(which you clicked).
if I got you correctly - both map and filter functions can give your callback second parameter - the index.
arr.map((n,i)=>{console.log(`[${i}]:${n}`)})
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(',')
I've spent the last couple hours going through some very similar answers to the above question but after a few implementations of loops and reduce I still have not gotten the solution I need.
I am getting an array of objects via service calls. I am pushing those controller array objects to another array because I need a single array to load data into a multi select. I.E.
StatesService.getAreaCities().then(function(response) {
controller.cities = response.data.rows;
controller.areaOptions.push(controller.cities);
});
StatesService.getAreaStates().then(function(response) {
controller.states = response.data.rows;
controller.areaOptions.push(controller.states);
});
controller.areaOptions = [];
This is an example of how I expect and need my array object to look.
With no modification this is how my array looks with those array objects pushed in.
How do I get the above data structure like the 1st image, an array of objects? I tried a solution with reduce()
var newCities = controller.cities.reduce(function(city){
return controller.city;
}, {});
controller.areaOptions.push(newCities);
but it wasnt what I was looking for because it returned a single object with all city property and all its values. I need each object to contain city and its value.
EDIT
I figured out the solution! Thanks to Lex.
I looped over each object in the array in each service and pushed the property.
Thanks to all for steering me in the right direction to figure this out, even the person that downvoted me :)
StatesService.getAreaCities().then(function(response) {
controller.cities = response.data.rows;
controller.cities.forEach(function(city){
controller.areaOptions.push(city);
});
});
Looks like response.data.rows is an array. So when you push response.data.rows into controller.areaOptions you are adding the rows array as an array element. (Basically making it a 2-dimensional array)
You should use Array.prototype.concat
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.
Using Chrome.
I've got a multidimensional array 'stories'
I'm printing 'stories' using console.log(stories);
Console log shows me all 24 elements of 'stories' in the console, but the first line of the console expansion arrow shows [remove:function]
When I try to print out to the console an individual array element using console.log(stories[2]);, it doesn't work. The console prints out undefined.
So, printing all array elements works just fine, but printing out an individual element doesn't.
Any idea what may be happening?
My end goal is to sort this array, and it is not working right now, and I am thinking that these issues are related.
Any advice greatly appreciated! thanks!
Further info:
I'm making an array and then passing elements into the array. each element is a sharepoint list item.
var stories = new Array();
getMyGPShareListData(".../ListProxy.html?listname=NewsArticles&odata=%24top%3D3%26%24orderby%3DArticleDate%20desc", { success: successGPNowData, error: errorGPNowData });
function successGPNowData(data) {
//Handle SharePoint REST List Items
if (data.d) {
iReturns = iReturns + 1;
//Determine data results to pass to top nav
jQuery.each(data.d, function (n, item) {
item.ArticleDate = item.ArticleDate.substr(6).replace(")/","");
stories.push(item);
});
}
}