Comparing elements in an object

I am working on a scraper that stores the results for json like this:

{"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"}]}

      

So, the first item in the results will be:

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

      

seconds will be

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

      

the third will be

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

      

Every time I clear the web it adds an element.

I would like to make a cleaner that removes an item if the stock and price are equal to the previous one, but only the previous ones, excluding the date.

In this example, since the third item is equal to the second, I would like to remove it. If 4r is 3 then remove it, etc.

+3


source to share


3 answers


DEMO

So, this way you can iterate your objects. Than you can erase an element over another:



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

      

0


source


You can easily do this with 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);
    });
}

      

0


source


here is the source code using 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 );

      

0


source







All Articles