简体   繁体   English

Javascript将两个json进行比较

[英]Javascript join two json and compare

I have this code: 我有以下代码:

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

so at the end you see that I have json and array_of_all_dates . 所以最后您会看到我有jsonarray_of_all_dates Now I want to join json to array_of_all_dates but if the same start element already excist into array_of_all_dates then remove it from array_of_all_dates and put this from json ... 现在,我想加入 jsonarray_of_all_dates但如果同样的start元素已经excist到array_of_all_dates然后从array_of_all_dates删除它,然后把这个从json ...

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

How to do this? 这个怎么做? please help. 请帮忙。

As far as I understand and interpret your question 据我了解并解释您的问题

Json is 杰森是

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

And array_of_all_dates is 并且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
   },
...
}

So you need to join these two, without having a duplicate "start" field. 因此,您需要将这两者结合在一起,而不必使用重复的“开始”字段。 My approach would be too merge the two JSON dataobjects, and remove the duplicate start field items. 我的方法将太过合并两个JSON数据对象,并删除重复的开始字段项。

Sample Implementation in JQuery: JQuery中的示例实现:

Lets say you append both the lists into one list called mergedItems then 假设您将两个列表都附加到一个称为mergedItems的列表中,然后

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

Edit: Working Example 编辑:工作示例

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

There is a handy javascript library http://underscorejs.org/ that you can use for this. 有一个方便的javascript库http://underscorejs.org/可用于此目的。 It's not exactly join, but it accomplishes the task with a single loop. 它不是完全联接,但它可以通过一个循环来完成任务。

  _.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;
      }
  });

I did notice there there were no matches because your date formats don't match exactly. 我确实注意到没有匹配项,因为您的日期格式不完全匹配。 You can try instantiating a new moment object in the findIndex predicate and specify a specific datetime format. 您可以尝试在findIndex谓词中实例化新的moment对象,并指定特定的日期时间格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM