比较对象中的元素

时间:2014-09-09 08:33:53

标签: javascript json object

我正在使用刮刀将结果保存到像这样的json:

{"Productos" : [
   {"Title":"Grabador de voz ISD1932","Results": [
      {"Stock":1,"Price":11.4,"Fecha":"18-8-2014:3:36"},
      {"Stock":1,"Price":12.4,"Fecha":"18-8-2014:3:38"},
      {"Stock":1,"Price":12.4,"Fecha":"19-8-2014:0:40"},
      {"Stock":1,"Price":12.4,"Fecha":"19-8-2014:21:46"},
      {"Stock":1,"Price":12.4,"Fecha":"21-8-2014:22:4"},
      {"Stock":1,"Price":12.4,"Fecha":"22-8-2014:0:40"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:0:48"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"}, 
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"}],
    "id":"4a1e90d7-e578-4bd5-b888-38c7bbfb4af5"}]}

结果中的第一个元素是:

{"Stock":1,"Price":11.4,"Fecha":"18-8-2014:3:36"} 

第二个是

{"Stock":1,"Price":12.4,"Fecha":"18-8-2014:3:38"} 

第三个是

{"Stock":1,"Price":12.4,"Fecha":"19-8-2014:0:40"} and so.

每当我刮网时,它都会添加一个元素。

如果库存和价格等于前一个元素,我想做一个清除元素,但只删除前一个元素,不包括日期。

在这个例子中,当第三个元素等于第二个元素时,我想删除它。如果4rth等于第3个,则将其删除,依此类推。

3 个答案:

答案 0 :(得分:0)

<强> DEMO

因此,通过这种方式,您可以迭代您的对象。比你可以删除一个元素与另一个元素相比:

var arr = {"Productos" : [
   {"Title":"Grabador de voz ISD1932","Results": [
      {"Stock":1,"Price":11.4,"Fecha":"18-8-2014:3:36"},
      {"Stock":1,"Price":12.4,"Fecha":"18-8-2014:3:38"},
      {"Stock":1,"Price":12.4,"Fecha":"19-8-2014:0:40"},
      {"Stock":1,"Price":12.4,"Fecha":"19-8-2014:21:46"},
      {"Stock":1,"Price":12.4,"Fecha":"21-8-2014:22:4"},
      {"Stock":1,"Price":12.4,"Fecha":"22-8-2014:0:40"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:0:48"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"}, 
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"},
      {"Stock":1,"Price":12.4,"Fecha":"23-8-2014:13:56"}],
    "id":"4a1e90d7-e578-4bd5-b888-38c7bbfb4af5"}]};

var data = arr.Productos[0].Results;

for(var i = 0; i != data.length; i++) {
    var stock = data[i].Stock;
    var price = data[i].Price;
    var fecha = data[i].Fecha;
    if( i < data.length - 1 && fecha === data[i + 1].Fecha) {
        data.splice(i + 1,1);
        i--;
    }
}

答案 1 :(得分:0)

您可以使用array.filter轻松完成此操作:

var data = ...;

for(var i = 0 ; i < data.Productos.length ; i++) {
    var p = data.Productos[i];
    p.Results = p.Results.filter(function(o, i, a) {
        return i == 0 || !(o.Stock == a[i - 1].Stock && o.Price == a[i - 1].Price);
    });
}

答案 2 :(得分:0)

这是使用javascript的原始代码:

 var  jsonObj  =  { "result" : {} }  
 var  results  =  {};  
 var  tempArr  =  [];  
for(var i = 0; i < 10; i++){  
  var stock = i+20;  
  var price = "14."+(i+10);  
  var fetch = '12'+i  
  results = {'stock' : stock, 'price' : price, 'fetch' : fetch };  
  if(i==0) tempArr.push(results);  
  else {       
  var lastObj = tempArr[ tempArr.length-1 ];  
  if( lastObj.stock == stock && lastObj.price == price ){  
     console.log("match found"); // if match found dont add  
  }  
  else tempArr.push( results );     
 }  
}  
jsonObj.result = tempArr;  
JSON.stringify( jsonObj );