我有這樣的代碼:的Javascript連接兩個JSON和比較
var json = '[{"id":113,"price":55,"start":"Sun, 24 Apr 2016 00:00:00 +0000","user_id":8},{"title":"","start":"2016-04-25T23:00:00.000Z","price":"115.7"},{"title":"","start":"2016-05-06T23:00:00.000Z","price":"115.7"},{"id":114,"price":45,"start":"Sun, 08 May 2016 00:00:00 +0000","user_id":9},{"title":"","start":"2016-05-08T23:00:00.000Z","price":"115.7"},{"id":111,"price":55,"start":"Wed, 01 Jun 2016 00:00:00 +0000","user_id":11},{"title":"","start":"2016-06-01T23:00:00.000Z","price":"115.7"},{"id":110,"price":53,"start":"Fri, 03 Jun 2016 00:00:00 +0000","user_id":8}]';
moment.utc().valueOf();
function fulljson(data) {
data = jQuery.parseJSON(data);
start = moment.utc('Sun, 24 Apr 2016 00:00:00 +0000').valueOf()/86400000;
start = parseInt(start);
console.log('pocetak'+start);
now = moment.utc().valueOf()/86400000;
now = parseInt(now);
console.log('sada'+now);
end = moment.utc('Sun, 05 Jun 2016 00:00:00 +0000').valueOf()/86400000;
end = parseInt(end);
console.log('kraj'+end);
auction_end = parseInt('3');
now = now + auction_end;
console.log('a sada je '+now);
if (start > now) {
pocetak = start;
console.log(pocetak);
} else {
pocetak = now;
console.log(pocetak);
}
var array_of_all_dates = [];
for (i = pocetak; i < end; i++) {
var new_object = {
title: '1',
start: moment(i*86400000).utcOffset('+0000').format(),
price: '{{$article->lowest_bid}}'
};
array_of_all_dates.push(new_object);
}
document.write(JSON.stringify(array_of_all_dates));
console.log(array_of_all_dates);
};
fulljson(json);
所以在最後你看,我有json
和array_of_all_dates
。現在我想加入json
到array_of_all_dates
,但如果同一start
元素已經excist到array_of_all_dates
然後從array_of_all_dates刪除它,然後把這個從json
......
http://jsbin.com/qekijumobe/edit?html,js,output
如何做到這一點?請幫忙。
只要是一定的我知道你在問什麼。您正嘗試** ** ** ** start **屬性上的數組。當你找到一個匹配的對象時,你想用'json'數組中相應的對象替換'array_of_all_dates'數組中的那個對象。 這是正確的嗎? –
是的,也可以使用array_of_all_dates的日期與moment.utc(json.start).valueOf()/ 86400000相同;從json ...所以你可以比較這兩個值 – Andrew
,但你也可以使用從兩個開始...比較 – Andrew