value of particular field not updating in for loop - javascript

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.

Related

How to change the currentText of repeated ComboBox in QML?

As I understand it, it is not possible to directly change the property currentText of a QML ComboBox. Instead, one needs to access it via currentIndex. However, I cannot seem to get it either. The JS code I need to update the text of a ComboBox with is the following:
function fillCombosFromHistory (s, rep1, rep2, replength) {
let u = s.replace(/\s+/g,'').split('&');
let v = [];
for (let i = 0; i < u.length; i++) {
v.push({
key: u[i].split('=')[0],
value: u[i].split('=')[1]
})
}
for (let j = 0; j < v.length; j++) {
for (let k = 0; k < replength; k++) {
if (v[j].key === rep1.itemAt(k).text) {
rep2.itemAt(k).model.currentIndex.text = v[j].value;
}
}
}
}
Here I pass the ids of two repeaters rep1 and rep2, where rep1 repeats Text (as a label) and rep2 repeats ComboBox. They are forcefully of the same length, so only one replength.
PS. I currently get the error TypeError: Value is undefined and could not be converted to an object.
So, I solved the problem. Apparently I was just confused about the properties of ComboBox. I changed the line
rep2.itemAt(k).model.currentIndex.text = v[j].value;
to
rep2.itemAt(k).editText = v[j].value;
and then some logic in other functions to accomodate the change and now everything works. Thanks anyway for the helpful hints!

Infinite Loop for finding a power set for a string

I'm working on a problem where I need to find all the power set of a given string which are all the possible subsets. I feel like I'm close with my current code but I can't figure out why I'm getting stuck on an infinite loop for my second iteration. I ran it through the debugger but I still can't seem to figure it out even though I'm sure it's very simple. When i = 0 then it goes to the second loop where j = 0 && j < 1 so for example if help is my given str argument then I would expect it to add j + '' and push it into my allSubsets array. The problem is that the j iteration will keep looping and doing j++ and will never stop. I'm not sure why this is. One particular question even if I solve this infinite loop - do I need to update the allSubsets.length in the iteration to keep it updated with the pushed in strings?
var powerSet = function(str) {
let allSubsets = [''];
for (let i = 0; i < str.length; i++) {
debugger;
for (let j = 0; j < allSubsets.length; j++) {
allSubsets.push(sortLetters(str[i] + allSubsets[j]));
}
}
return allSubsets;
};
var sortLetters = (word => {
//convert string to an array
//use the sort to sort by letter
//convert array back to string and return
return word.split('').sort().join('');
})
Everytime you push to allSubSets, the length increases, and thus, your loop never ends. A declarative loop runs on the range of the initial loop. See below for a fix based on your code:
var powerSet = function(str) {
let allSubsets = [''];
for (let i = 0; i < str.length; i++) {
allSubsets.forEach( (_char, j) => { // declarative loop here
allSubsets.push(sortLetters(str[i] + allSubsets[j]));
})
}
return allSubsets;
};
var sortLetters = (word => {
return word.split('').sort().join('');
})
From MDN web docs:
The range of elements processed by forEach() is set before the first invocation of callback. Elements which are appended to the array after the call to forEach() begins will not be visited by callback. If existing elements of the array are changed or deleted, their value as passed to callback will be the value at the time forEach() visits them; elements that are deleted before being visited are not visited. If elements that are already visited are removed (e.g. using shift()) during the iteration, later elements will be skipped. (See this example, below.)
See the fourth paragraph under descriptions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description

let i always equals undefined but var i is defined in for loop

I am using vue, vuedraggable and vuetify in my project.
I am not able to use let to define my index for my loop it always is undefined. This method is being called by an event from draggable. Using var instead of let works however.
Why is let always undefined?
Even when I directly assign the variable i to 1 it is still undefined. For example for(let i = 1; i < 2; i++) still results in i being equal to undefined inside the for loop.
This is where I am seeing the issue
updateOrderNumbers(draggedContext) {
if (draggedContext.index < draggedContext.futureIndex) {
for (let i = draggedContext.index; i < draggedContext.futureIndex; i++) {
let swapIndex = this.orderTableData[i].sortOrder;
this.orderTableData[i].sortOrder = this.orderTableData[i+1].sortOrder ;
this.orderTableData[i+1].sortOrder = swapIndex;
}
}
if (draggedContext.index > draggedContext.futureIndex) {
for (let i = draggedContext.index; i > draggedContext.futureIndex; i--) {
let swapIndex = this.orderTableData[i].sortOrder;
this.orderTableData[i].sortOrder = this.orderTableData[i+1].sortOrder;
this.orderTableData[i+1].sortOrder = swapIndex;
}
}
}
This method gets called from here
onMoveCallback(evt){
this.updateOrderNumbers(evt.draggedContext)
this.checkForChanges()
}
And here is the element that is triggering the call.
<draggable v-model="orderTableData" :move="onMoveCallback" tag="tbody">
It appears that FireFox and Chrome are unable to determine the value of a variable initialized with let inside of a for loop in this circumstance, but they are able to determine its value if you initialize the variable with var inside of the for loop.

How to get loop value outside the loop

How to get the loop values outside the loop below example only prints the last value, For example if i wanted to print the loop results.
var result;
for (var i=0; i < 10; i++) {
result = i;
}
console.log(result);
Now how can i get the iterated values of the loop which are (1 to 10) in the console, because now i will only print the last value which is 10.
Put the log statement inside the loop where you set the value.
var result;
for (var i=0; i < 10; i++) {
result = i;
console.log(result);
}
If you only want one output statement, you can concatenate your results before logging:
var result = "";
for (var i=0; i < 10; i++) {
result += i + " ";
}
console.log(result);
This will output 0 1 2 3 4 5 6 7 8 9 10
If you really want to log outside of the loop, wich is quite unnecessary in my opinion, may use an array? :
var result=[];
for (var i=0; i < 10; i++) {
result.push(i);
}
console.log(...result);
http://jsbin.com/gogeluhavi/edit?console
If you want result make to log magically, you may uses setters and a Proxy, so called Observables.
Enter result=10 into this console, ive implemented a Observable for you: http://jsbin.com/xacujabuwo/edit?console ; You could also paste your for loop...
The Above answears are correct but I would like to clear it up for you so you understand it too.
In your original code you have declared the variable "result" outside the loop, then at each iteration a value is assigned to your variable. So that first time around "result" = 0 , second loop and the "result" = 1 and so on.
When the the for loop is finished it reads the next line which is your console.log() statment with the variable "result". The last time the variable "result" was modified was inside the for loop at the 10th iteration and thats where you let it equal to the iterator,therefore the value of iterator is 11.
In order to display something at every iteration, the console.log() must be placed inside the loop. Check below example
var result;
for (var i=0; i < 10; i++) {
result = i;
console.log(result); // prints at every iteration
}
Since you didnt add jQuery tag I used only javascript.
Add a input hidden tag
<input id="idOfInput" style="visibility:hidden" type="text">
Set the desired value to the input
for (var i=0; i < 10; i++) {
result = i;
document.getElementById('idOfInput').value = result;
document.getElementById('idOfInput').change(); //note change() is to trigger the event
}
Add change event listener and get the value set in loop
var input = document.getElementById('idOfInput');
input.addEventListener('input', function()
{
console.log('input changed to: ', input.value); //you get the value here
});
hope it helps
var result=[];
for (var i=0; i <= 10; i++) {
result.push(i);
}
console.log("Result =>", result);

For loop adding undefined entry's

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
});
}

Categories