1

I have these data array dataTimeName

{"imsak":"04:44","fajr":"04:54","sunrise":"06:15","dhuhr":"12:17","asr":"15:40","maghrib":"18:19","isha":"19:31"}

and the JS

$.each(dataTimeName,function(key, val)
{
    var current = "15:40";
    var previousArray = ""; //<-- how to get the previous from current?
});

My question is how to get the previous value from the current 15:40 from dataTimeName list?
It means, I will get previous value before 15:40 is 12:17.

var dataTimeName  = {"imsak":"04:44","fajr":"04:54","sunrise":"06:15","dhuhr":"12:17","asr":"15:40","maghrib":"18:19","isha":"19:31"};

$.each(dataTimeName,function(key, val)
{
  var current = "15:40";
  var previousArray = ""; //<-- how to get the previous from current?
  alert(val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

3
  • Maybe array makes sense to have this order. You can convert your object times into an array and then access the previous time. Commented Jan 30, 2021 at 9:02
  • Hi @HassanImam would you give the example? Commented Jan 30, 2021 at 9:12
  • Maybe something like this [ "04:44", "04:54", "06:15", "12:17", "15:40", "18:19", "19:31" ] Commented Jan 30, 2021 at 9:12

3 Answers 3

1

You can add your JSON values inside some array and use .findIndex() to get index of current(which is search) then just subtract -1 and access previous value.

Demo Code :

var dataTimeName = {
  "imsak": "04:44",
  "fajr": "04:54",
  "sunrise": "06:15",
  "dhuhr": "12:17",
  "asr": "15:40",
  "maghrib": "18:19",
  "isha": "19:31"
};
var currents = "15:40";
var ids = []
$.each(dataTimeName, function(key, val) {
  ids.push(val);//push values inside array
});
console.log(ids)
//find index where value matches - 1 (to get previous value)
var position = ids.findIndex(x => x === currents) - 1
console.log(ids[position])//access value like this
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

You have a sorted array so you can access it's previous index:

let dataTimeName  = {"imsak":"04:44","fajr":"04:54","sunrise":"06:15","dhuhr":"12:17","asr":"15:40","maghrib":"18:19","isha":"19:31"};

let current = "15:40"

// sorted array
let item_index = Object.values(dataTimeName).findIndex(v=>v===current);
let previousArray=Object.entries(dataTimeName).filter((_,index)=>index<item_index);

console.log(previousArray);

// general
console.log(
  Object.entries(dataTimeName)
  .filter(([key,value])=>parseInt(value.replace(':', '')) < parseInt(current.replace(':', '')))
)

// And if you need the result be a json object like first one:

console.log(
  Object.entries(dataTimeName)
  .reduce((r,[key,value])=>{
      if (parseInt(value.replace(':', '')) < parseInt(current.replace(':', ''))) r[key]=value;
      return r;
    }
  ,{})
)

// And if you need only the previous time:
let prev = Object.values(dataTimeName).filter((_,index)=>index<item_index)?.pop();
console.log(prev);

2 Comments

Hi, just now tried your code. But I only need last previous result. So what expected is only get 12:17
You wrote: previousArray so i wrote this, i'll update
0

You could store the value of the previous iteration outside of the foreach function and update it at the end of the function.

But maybe if your object have to be ordered in a specific way it might be easier to store it as a classical array so you could access the previous value by dataTime[key-1]

Also be carefull because the previous value will not be defined for the first iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.