jquery - Javascript loop and insert new object -


i ajax data:

[{"id":125,"price":225,"start":"tue, 26 apr 2016 00:00:00 +0000","user_id":8},{"id":124,"price":200,"start":"wed, 27 apr 2016 00:00:00 +0000","user_id":8},{"id":121,"price":67,"start":"sat, 23 apr 2016 00:00:00 +0000","user_id":8},{"id":114,"price":45,"start":"sun, 08 may 2016 00:00:00 +0000","user_id":9},{"id":113,"price":55,"start":"sun, 24 apr 2016 00:00:00 +0000","user_id":8},{"id":111,"price":55,"start":"wed, 01 jun 2016 00:00:00 +0000","user_id":11},{"id":110,"price":53,"start":"fri, 03 jun 2016 00:00:00 +0000","user_id":8},{"id":107,"price":53,"start":"wed, 03 aug 2016 00:00:00 +0000","user_id":8},{"id":106,"price":55,"start":"mon, 01 aug 2016 00:00:00 +0000","user_id":8},{"id":105,"price":53,"start":"tue, 16 aug 2016 00:00:00 +0000","user_id":8},{"id":104,"price":55,"start":"thu, 21 apr 2016 00:00:00 +0000","user_id":11},{"id":101,"price":57,"start":"wed, 17 aug 2016 00:00:00 +0000","user_id":8}] 

so can see there start data. have:

var start = '04/17/2016 12:00 am'; var end = '09/20/2016 12:01 am'; 

now need make loop day day start end, check if day(date) excist json , if not add new data json :

{"id":0,"price":100,"start":"date","user_id":0} 

what best way that?

update: create 1 new dates start end need put json new array: http://jsbin.com/qekijumobe/edit?js,output

my answer had fault in there, came after it:

first, create foreach loop, wich loops trough of objects inside array

for (var obj of objarray) {} //use "of" instead of "in" tk values , not indices 

then, inside loop, check every field of objects, if they're same, abd if they're, break out loop, else, if there's no matching object, push in array.

function isinarray(yourobj) {     (var obj of objarray) {         if (obj.id == yourobj.id && obj.start == yourobj.start && obj.price == yourobj.price && obj.user_id == yourobj.user_id)              return true;      }     return false; } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -