I'm using a couple of for loops to create an array. Problem is, on the second pass it adds 4 undefined variables and I can't see where they're coming from.
Note: the if statement is correct and only gets fired when they match.
The code :
for (var x = 0; x < galleryObject[1].length; x++) {
gallerys[x]= [];
for (var i = 0; i < galleryObject[2].length; i++) {
if (galleryObject[2][i].galId === galleryObject[1][x].id) {
gallerys[x][i] = {};
gallerys[x][i].filename=galleryObject[2][i].fileName
gallerys[x][i].caption =galleryObject[2][i].caption
}
}
}
Obviously the problem here in the fact that sometimes your IF statements returns false. In that case it tries to add an element to an array but some previous indexes are not specified, so it fills them with 'undefined'.
Try to change your code in the IF statement to:
if (galleryObject[2][i].galId === galleryObject[1][x].id) {
gallerys.push({
filename:galleryObject[2][i].fileName,
caption :galleryObject[2][i].caption
});
}
Related
I have two for loops,the value inside inner for loop is not updating with the value of outer for loop.
I have 2 arrays,
let getExhibitors= [{"_id":"5c78102829c1cc00082c9956","title":"Accenture","sponsorSortOrder":1,"sortOrder":1,"__v":1,"beconDetails":[],"productDemos":[],"sponsorTags":[],"exhibitorTags":[],"eventId": "5c78088a29c1cc00082c990b","entityId": "5c78102829c1cc00082c9956","favourite":true,"notes":'hey it is exhibitor'}]
let exhibitorsArray= [{"_id":"5d7797029f3ae4000821d2df","favourite":true,"entityId":"5c78109529c1cc00082c9959","module Id":"EXHIBITORS_MODULE","eventId":"5c78088a29c1cc00082c990b","__v":0,"modifiedDate":"2019-09-10T12:34:48.993Z","creationDate":"2019-09-10T12:28:50.526Z","comments":"","notes":"",},{"_id":"5d5cf3d8adaac20007cbcc12","favourite":false,"entityId":"5c78102829c1cc00082c9956","moduleId":"EXHIBITORS_MODULE","eventId":"5c78088a29c1cc00082c990b","__v":0,"modifiedDate":"2019-09-17T10:04:03.891Z","creationDate":"2019-08-21T07:33:44.077Z","comments":"","notes":"hey it is exhibitor","id":"5d5cf3d8adaac20007cbcc12"}]
for(let i=0;i<exhibitorsArray.length;i++){
console.log("inside first forloop ",exhibitorsArray[i]);
for(let j=0;j<getExhibitors.length;j++){
console.log("inside second forloop",getExhibitors[j]);
if((exhibitorsArray[i].entityId==getExhibitors[j].entityId ) && (exhibitorsArray[i].eventId==getExhibitors[j].eventId)){
console.log("exhibitor present",getExhibitors[j],exhibitorsArray[i]);
getExhibitors[j].favourite=exhibitorsArray[i].favourite
getExhibitors[j].notes=exhibitorsArray[i].notes
console.log("exhibitors final",getExhibitors);
}
}
}
here value of exhibitorsArray[i].favourite is not assigning to getExhibitors[j].favourite and value of exhibitorsArray[i].notes is not assigning to getExhibitors[j].notes,I mean ,value inside the console "exhibitors final" is retaining the same,not updating.
Please help me out to solve it.
One of the keys you are using is wrong (entityId is _id).
You didn't provide loadedSynchData so I cannot properly test my code.
Please try the following:
for (let i = 0; i < exhibitorsArray.length; i++) {
for (let j = 0; j < getExhibitors.length; j++) {
if ((exhibitorsArray[i].entityId == getExhibitors[j]._id) && (loadedSynchData[i].eventId == getExhibitors[j].eventId)) {
console.log("exhibitor present", getExhibitors[j], exhibitorsArray[i]);
getExhibitors[j].favourite = exhibitorsArray[i].favourite
getExhibitors[j].notes = exhibitorsArray[i].notes
}
}
}
This is why assignment operation failed.
Both getExhibitors[j].entityId and getExhibitors[j].eventId do not exist at all. You should also check if this,loadedSynchData[i].eventId really exists since the data for this array loadedSynchData is not provided yet.
Basically i have to create a quiz with 3category. each with 5questions.
I would have to push the selected category-questions into this new array from the array with all the questions. I am unable to do so.
pushSelectedQuestion() {
for (var i = 0; i < this.getNumberOfQuestion; i++) {
if (usercategory == questionPool[i].category) {
mcqSelected.push(questionPool[i])
return mcqSelected;
}
}
}
usercategory = input from user.
if user chooses category 1.
if (1 == questionPool[1].category) (if it matches the category) then it will be pushed.
This is the part which i cant do
Well, from the information you've provided, there's one main issue here - the return statement is definitely shortcutting the loop - so even if you have everything else right, you'll only ever get the first matching question. The rest will have been cut out by the return statement, which stops the function and returns the value.
pushSelectedQuestion() {
for (var i = 0; i < this.getNumberOfQuestion; i++) {
if (usercategory == questionPool[i].category) {
mcqSelected.push(questionPool[i])
// the below line is causing this loop to end after the first time through the list.
// Remove it and then put a console.log(mcqSelected);
// here instead to see the value during each iteration of the loop.
return mcqSelected;
}
}
}
There are a lot of ways to accomplish what you want to do here though. For example, you could just use the javascript Array.filter method like so
let selectedQuestions = questionPool.filter(question => question.category == userCategory)
Maybe I am not understanding your question correctly, but can't you use nested arrays. If the questions are categorized beforehand that is.
I have the following checkbox who is calling a function on launch to get checked based on a value that comes in from an api (cat.term_id)
<input ng-checked="ifCatExcluded(cat.term_id)">
The function works as follows: The first bit sets the checkbox value to true and gets an array which i need to cross check the id.
$scope.ifCatExcluded = function(sent_id){
var doesNotExist = true;
arraytosearch = JSON.parse(localStorage.getItem("categoryListMem"));
The second bit creates an empty array and adds the cat.term_id to this new array to avoid re running this id later.
if($scope.tempArray == undefined || $scope.tempArray.indexOf(undefined) > 0) {
$scope.tempArray = [];
}
if ($scope.tempArray.indexOf(sent_id) < 0) {
$scope.tempArray.push(sent_id);
console.log($scope.tempArray);
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i]['id'] == sent_id) {
doesNotExist = false;
console.log(sent_id + " IS EXCLUDED");
}
}
}
Then the last part returns the value for the checkbox and here is the problem. I cannot get a return of true or false from the doesNotExist variable. I noticed that when I take
for (var i = 0; i < arraytosearch.length; i++) {" ecc...
out of the if statement everything works...
return doesNotExist;
console.log(doesNotExist);
};
Any help is much appreciated.
i wanna generate a 3x3 field. I want to do this with JS, it shall be a web application.
All fields shall inital with false. But it seems so that my code is not working correctly, but i don't find my fault. The goal is, that every spacesector is accessible.
Thats my idea:
// define size
var esize = generateSpace(3);
}
space[i] = false is replacing the array with a single boolean value false, not filling in all the entries in array you just created. You need another loop to initialize all the elements of the array.
function generateSpace(x) {
var space = [];
for (var i = 0; i < x; i++) {
space[i] = [];
for (var j = 0; j < x; j++) {
space[i][j] = false;
}
}
return space;
}
Also, your for() loop condition was wrong, as you weren't initializing the last element of space. It should have been i < space.length.
And when it's done, it needs to return the array that it created.
Since I got somewhat bored and felt like messing around, you can also initialize your dataset as shown below:
function generateSpace(x) {
return Array.apply(null, Array(x)).map(function() {
return Array.apply(null, Array(x)).map(function() {
return false;
});
});
}
The other functions work equally well, but here's a fairly simply looking one using ES6 that works for any square grid:
function generateSpace(x) {
return Array(x).fill(Array(x).fill(false));
}
I am creating a 'simple' javaScript function which basically displays new information on the page when a user clicks next or previous.
The information is taken from an array and I want to use either i++ or i-- to call an element of the array.
Heres my JavaScript:
var titles = ["Dundalk", "Navan", "Drogheda", "Dublin"];
var i = 0;
function next()
{
i++;
if (i == titles.length)
{
i = 0;
}
var object = document.getElementById('tname');
object.innerHTML = titles[i];
}
function prev()
{
if (i == 0)
{
i = titles.length;
}
i--;
var object = document.getElementById('tname');
object.innerHTML = titles[i];
}
The problem is, when I run this code in my HTML page, I get an 'UNDEFINED' result. The JavaScript is not recognizing that i has been initialized as 0 in the beginning.
If i change titles[i] to titles[2], for example, the correct text is displayed in HTML.
What am I forgetting or how can I overcome this?
Thanks
The fact that you're seeing undefined indicates that you're accessing an array index which hasn't been set. Your code looks fine at a glance, so I would guess that there's some more code you're not showing which also uses i as a loop variable and leaves it set to a value > titles.length after the code above has run.