finding if all items in array are set to expected value in javascript? [on hold]


finding if all items in array are set to expected value in javascript? [on hold]



I am fairly new to javascript and I am learnig by doing a project.
I have a situtation in which I collect statuses of various tasks to an array.



var statuses = ["deployed", "deployed", "pending", "staged"];


var statuses = ["deployed", "deployed", "pending", "staged"];



now I want to call clearInterval(interval) when all the items in the array are deployed


clearInterval(interval)


array


deployed



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Please take a few minutes to read through "how to ask a good question" again, and then come back to your post. It's lacking a lot of details that you'd add if you were asking someone in person, so: please add those details.
– Mike 'Pomax' Kamermans
2 days ago






since strings take up more space, I would consider using ints to represent your data, and then have a Map of <int, string> to display if need be. BUT, you would need to do some sort of: setTimeout(fn, 500); where fn walks the array.
– Fallenreaper
2 days ago


setTimeout(fn, 500);


fn





@Fallenreaper I will higly appreciate if you can show an example how you suggested using map of <int, string> the list in my case is dynamically generated
– Ciasto piekarz
2 days ago






@Ciastopiekarz: Can you elaborate further? Like when you planning to call clearInterval(interval)? Since you mentioned when all the items are deployed, how often you check this? Periodically? Or one off thingy?
– Isaac
2 days ago


clearInterval(interval)


when all the items are deployed




5 Answers
5



Use the every method:


every


if (statuses.every(status => status === "deployed")) {
clearInterval(interval);
}




var statuses = ["deployed", "deployed", "pending", "staged"];

if(statuses.every(x=>x === "deployed")){
//clearInterval(interval);
}

console.log(statuses.every(x=>x === "deployed"))



You can use every to check if all elements in array are deployed. The above code is based solely on your description I want to call clearInterval(interval) when all the items in the array are deployed


every


deployed



Iterate over 'statuses', checking each current value for NOT "deployed". The first time that condition is met, you know you do not need to perform the 'clearInterval' logic. This means that if the condition is never met, you can proceed with that logic.



So here is what I think you should do, and it works pretty well. Ill break everything down so it makes sense.


/// I just dont like using strings when ints will suffice, so im using 0,1,2 instead of your defined strings.
var valueRef = { 0: "Deployed", 1: "Pending", 2: "Staged" };
/// Info contains your sample data.
var info = [0,0,1,2]
/// This will be needed to clear the repeating fn call
var timeoutKey;
function checkIfAllDeployed(){
/// Get the size of your info array
var len = info.length;
for (index in info){
/// Loop through all items in the array to check to see if they are 0 (deployed)
if (info[index] == 0){
/// it is 0, subtract 1.
len --;
}
}
/// since you have the length (this sample is 4), when the function finally runs and they are all deployed, then len will now be 0 here.
if (len <= 0){
/// Turn off the timeout.
clearTimeout(timeoutKey);
}
/// I use bind so that what the scope is the same as the parent, so it has access to timeoutKey
}.bind(this);
/// Sets your timeout. This is where you kick it all off at. It will run at an interval of whatever you want. 1000 == 1 second. So 500 = 0.5 seconds.
timeoutKey = setInterval(checkIfAllDeployed, 500)



Now, that covers loops, but since you are doing ES6, there are some cool tricks such as the every function for arrays. It takes a function to compare.


every


var timeoutKey
function checkIfAllDeployed(){
if (info.every(function(item){ return item == 0}){
clearTimeout(timeoutKey)
}
}
setInterval(checkIfAllDeployed, 500);



Or if you want to use shorthand:


var timeoutKey
function checkIfAllDeployed(){
/// Now in my example, i use 0, but you can use the string "deployed" here.
if (info.every(x => x == 0){
clearTimeout(timeoutKey)
}
}
setInterval(checkIfAllDeployed, 500);



Now the reason I use INTS etc is that it takes up less space. There are far less strings. Imagine a HUGE array of strings. Not too good, but with a map you can easily rereference.



If you decide you every need data for a given row you can just say:


valueRef[info[index]]



and it will give you the String you are looking for. This is kinda like an enum, Which you could also used if you wanted. Since you have a static set of states, you could just create an enum and it would work similar to the map example i gave you.



Ciasto!



From my understanding, you are just trying to iterate through the statuses array and verify that each element of that array only contains the strings "deployed". This code below is a simple implementation of that and I've inserted a few console log statements to help show the logic of stepping through the process.


function isDeployed(array) {
for (var i = 0; i < statuses.length; i++) {
if (statuses[i] !== 'deployed') {
return false;
}
}
return true;
}

var statuses = ["deployed", "deployed", "pending", "staged"];
// var statuses = ["deployed", "deployed", "deployed", "deployed"];

var result = isDeployed(statuses);
console.log(result);

if (result) {
console.log('Add more code to deal with your next steps');
}



Let me know if you need further clarification or if this isn't quite what you were asking!

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift