簡體   English   中英

Javascript將兩個json進行比較

[英]Javascript join two json and compare

我有以下代碼:

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);

所以最后您會看到我有jsonarray_of_all_dates 現在,我想加入 jsonarray_of_all_dates但如果同樣的start元素已經excist到array_of_all_dates然后從array_of_all_dates刪除它,然后把這個從json ...

http://jsbin.com/qekijumobe/edit?html,js,輸出

這個怎么做? 請幫忙。

據我了解並解釋您的問題

傑森是

[  
   {  
      "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"
   },
..
}

並且array_of_all_dates是

[  
   {  
      "title":"1",
      "start":"2016-04-24T00:00:00+00:00",
      "day":16915,
      "price":100
   },
   {  
      "title":"1",
      "start":"2016-04-25T00:00:00+00:00",
      "day":16916,
      "price":100
   },
   {  
      "title":"1",
      "start":"2016-04-26T00:00:00+00:00",
      "day":16917,
      "price":100
   },
...
}

因此,您需要將這兩者結合在一起,而不必使用重復的“開始”字段。 我的方法將太過合並兩個JSON數據對象,並刪除重復的開始字段項。

JQuery中的示例實現:

假設您將兩個列表都附加到一個稱為mergedItems的列表中,然后

var uniqueItems = [];
$.each(mergedItems["start"], function(i, el){
    if($.inArray(el, uniqueItems ) === -1) uniqueItems.push(el);
});

編輯:工作示例

http://jsbin.com/vakodahile/edit?html,js,輸出

有一個方便的javascript庫http://underscorejs.org/可用於此目的。 它不是完全聯接,但它可以通過一個循環來完成任務。

  _.each(jQuery.parseJSON(json), function(d){    

      console.log(d.start)  
      var idx = _.findIndex(array_of_all_dates, function(ad){ return d.start == ad.start });

      if(idx>-1){
          array_of_all_dates[idx] = d;
      }
  });

我確實注意到沒有匹配項,因為您的日期格式不完全匹配。 您可以嘗試在findIndex謂詞中實例化新的moment對象,並指定特定的日期時間格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM