Each day a different item in an array - javascript

javascript array
javascript array methods
javascript multidimensional array push
js array last
javascript last element in array es6
javascript reduce
javascript map
array of images javascript w3schools

I have this script that makes a button redirect to a whatsapp page, on the URL (a href) I need to insert the number that's gonna be contacted.

What I need to do is each day a different number fills this URL. Example:

day1 - phonen1,

day2 - phonen2,

...,

day 13 - phonen13,

//(starts over)

day14 - phonen1,

day15 - phonen2,

...

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>

    phones= ["phonen1", "phonen2", ..., "phonen13"];
    document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+ phones[i] +"";  

</script>

you can use the date object with for loop like this:

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>
   phones= ["phonen1", "phonen2", ..., "phonen13"];
   var d = new Date();
   var todayDate = d.getDate();

   for (var i = todayDate; i > 13; i= i-13) {
     todayDate = todayDate - 13;
   }

   document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+phones[i] + todayDate;
</script>

JavaScript & DHTML Cookbook: Solutions & Examples for Web Programmers, The Solution displays a potentially different saying each time the user loads the a new Date object and get the day number to use as an index for the array of  The JavaScript method toString () converts an array to a string of (comma separated) array values. The join () method also joins all array elements into a string. It behaves just like toString (), but in addition you can specify the separator: When you work with arrays, it is easy to remove elements and add new elements.

Simple Answer:

You can do this using a Date to count the number of days since the unix epoch, and mod that count by the length of your phones array to get an index that moves to the next item every 24 hours:

let phones = ["phonen1", "phonen2", "phonen3", "phonen4"];

const ms_per_day = 24 * 60 * 60 * 1000;
// (new Date()).getTime() gets the number of ms since 1 January 1970 00:00:00 UTC
// we divide by ms_per_day and floor to get the number of 24-hour cycles (this will increment each UTC day)
let days_since_epoch = Math.floor((new Date()).getTime() / ms_per_day);
// we mod by the length of phones to get a number in the range [0, phones.length)
let phones_index = days_since_epoch % phones.length;

document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519" + phones[phones_index];

console.log("Set link to", document.getElementById("whatsapp").href);
<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734"> Link  </a>

Learn JavaScript Multidimensional Array By Examples, This tutorial shows you how to create a JavaScript multidimensional array by using an and the second one shows the number of hours spent per day for each. For example, to add a new element to a multidimensional array, you use the  First, rewrite your array like this: var items = [523,3452,334,31,5346]; Next, access the element in the array through its index number. The formula to determine the index number is: n-1. To replace the first item (n=1) in the array, write: items[0] = Enter Your New Number; In your example, the number 3452 is in the second position (n=2).

Use the date object to create an index into your array

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>

  var phones= ["phone1","phone2","phone3","phone4","phone5","phone6","phone7","phone8","phone9","phone10","phone11","phone12","phone13","phone14"];
    
  var startOfDay1 = new Date('July 1, 2018 00:00:00');//be aware this is the client timezone
  var diffFromNow = Date.now() - startOfDay1.getTime();//get the difference in ms between now and midnight of "day 1"
  console.log(diffFromNow);
  var diffFromNowDays = diffFromNow/(24*60*60*1000);//turn that into a day value
  var daynum = Math.floor(diffFromNowDays % 14);//constrain to 14 days
  console.log(daynum);//zero based index
    document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+ phones[daynum] +"";  

</script>

Arrays List Days of the Week, introduction to javascript. An Array() is similiar to a variable holding one data item whereas the array stores multiple values with an var dayNames = new Array("Sunday", "Monday",) The fifth day of the week is dayName[4] Thursday JavaScript for loops iterate over each item in an array. JavaScript arrays are zero based, which means the first item is referenced with an index of 0. Referencing items in arrays is done with a numeric index, starting at zero and ending with the array length minus 1.

Ollin's answer is great, but you can use local midnight as follows if you wish. Use the remainder operator % with the number of whole days since a particular point in time, any epoch will do.

If you want to do the changeover at midnight local time, then use local midnight for the epoch and current day. Use Math.round to remove daylight saving effects.

The following will change the value returned from the array at local 00:00:00.001 each day:

// Number of whole local days since date to today
function daysDiff(date) {
  // Copy date, set to start of day
  var d = new Date(+date);
  d.setHours(0,0,0,0);
  // Get start of current day
  var e = new Date();
  e.setHours(0,0,0,0);
  // Return whole day count
  return Math.round((e - d)/8.64e7);
}

// Select item from array based on number of whole days
// from epoch to today. Default is 1 July 2018
function getFromArray(array, epoch) {
  if (!epoch) {
    // If not provided, use 1 July 2018
    epoch = new Date(2018,6,1);
  }
  var d = daysDiff(epoch);
  return array[d % array.length];
}

var nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];

// On local date 12 July 2018 returns 12
console.log(getFromArray(nums));

The complete guide to JavaScript Array, But unfortunately in javascript array data structure is bit different… Array.​prototype.reverse() — this method reverses the order of the elements of an existing array i.e. the var everyDay = (Learning) => eval(new Me(Learning + Experience));. One of the most popular methods of iterating through datasets in JavaScript is the .map() method. .map() creates an array from calling a specific function on each item in the parent array. .map() is a non-mutating method that creates a new array inste

Useful Javascript Array and Object Methods, Creates a new array by manipulating the values in another array. Great for data manipulation and it is often used in React because it is an  JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: However, what if you want to loop through the cars and find a specific one?

5 Array Methods That You Should Be Using Now, If you sniff around popular open-source JS projects on Github, you The filter() method creates a new array with all elements that pass the test But still I didn't see much use for it until I was refactoring my code one day and it  I have JavaScript object array with the following structure: I want to extract a field from each object, and get an array containing the values, for example field foo would give array [ 1, 3, 5 ]. I can do this with this trivial approach: Is there a more elegant or idiomatic way to do this, so that a custom utility function would be unnecessary?

JavaScript: The Missing Manual: The Missing Manual, At the end of the loop (the last brace), the JavaScript interpreter goes back to the come in handy when dealing with a common JavaScript element—an array. to the check out counter (in other words, move to the next step of your “program”). using JavaScript, and you want to print the name of each day of the week on  The forEach () method calls a function once for each element in an array, in order. Note: the function is not executed for array elements without values. Browser Support. The numbers in the table specify the first browser version that fully supports the method. array .forEach ( function (currentValue, index, arr), thisValue) Parameter Values.

Comments
  • What issues are you running into?
  • I dont know how to make the array print a different number each day.
  • why can't you use Math.random()?
  • Because it can't be random, it needs to follow the rule as the example I showed.
  • phones[d % phones.length] where d is the day number, starting with d = 0
  • This cant help me, because the array are actually real phone numbers, and also after the 13th day, it begins from position 0 again.
  • ok you can still use the array and just add a small if statement to correct things out I edited my answer
  • Best answer so far, ... Good job :)
  • Rather than using a non–standard string that must be parsed by the built-in parser, if you wish to create a date with specific values then pass them directly to the constructor: new Date(2018, 6, 1). It's less to type and avoids the (notoriously unreliable) parser completely. ;-)