0
[ { start_time: '1', end_time: '10' },
  { start_time: '3', end_time: '15' } ] 

These object are multiple...

This is my time range array, if the first object has start_time 1 and end_time is 10 then the second object start_time, not between in 1 to 10.

I want the second object start_time greater than the first object end_time.

Expected output.

[ { start_time: '1', end_time: '10' },
{ start_time: '11', end_time: '15' } ] 
4
  • You can't, at least not with the provided data. Commented Feb 24, 2020 at 6:09
  • Please check expected output. Commented Feb 24, 2020 at 6:12
  • hey @HirdeshSingh! can you show us what you tried so far? so we can guide you further? Commented Feb 24, 2020 at 6:13
  • How are the output values related to the input values? This doesn't make any sense ... Commented Feb 24, 2020 at 7:07

4 Answers 4

1

You simply need to traverse through object elements and modify data if its less than what you need:

var data = [ { start_time: '1', end_time: '10' },
  { start_time: '3', end_time: '15' } ] ;
  
var temp = null;
for(var index=0; index< data.length; index++) {
   if (temp && parseInt(data[index].start_time) <= parseInt(temp)) {
      data[index].start_time = (parseInt(temp)+1).toString();
   }
   temp = data[index].end_time;
} 

console.log(data);

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

Comments

1

Use temporary variable (say time) and compare start_time and time before push to new updated array.

const data = [
  { start_time: "1", end_time: "10" },
  { start_time: "3", end_time: "15" }
];

let time = -Infinity;
const updated = [];

data.forEach(({ start_time, end_time }) => {
  updated.push({ start_time: String(Math.max(time, start_time)), end_time });
  time = Number(end_time) + 1;
});

console.log(updated);

2 Comments

Thank you but I want if first object start_time is 1 and end_time is 10 then all other objects must have start_time greater then the end_time of the first object if it is not then it must return an error.
@HirdeshSingh You should mention this explicitly in your question. This is not the place to mention this.
0

Use this:

 var arr = [ 
   { start_time: '1', end_time: '10'},
   { start_time: '3', end_time: '9'},
   { start_time: '5', end_time: '10'},
   { start_time: '10', end_time: '15'} 
 ];

 var modifiedArr = arr.map((item, index)=>{ return checkObject(item,index);})

 function checkObject(item, index) {
  if(index == 0) return item;
  let prevItem = arr[index-1];
  if(parseInt(item.start_time) < parseInt(prevItem.end_time)){
    item.start_time = parseInt(prevItem.end_time) + 1;
  }
  if(parseInt(item.end_time) <= parseInt(item.start_time)){
   item.end_time = parseInt(item.start_time) + 1;
  }

  return item;
 }
 console.log(modifiedArr);

Comments

0

I have tried some initial code with simple if condition code. You can refer it whether it will work for you. kindly check the following code.

 for (var i = 0; i < obj.length - 1; i++) {
     if (Number(obj[i + 1].start_time) <= Number(obj[i].end_time)) {
         alert("start_time must be greater than previous end_time");
     }
 }

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.