3

i am working on a scraper that saves results to a 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 first element in results would be:

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

second would be

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

third would be

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

Every time i scrape the web, it adds an element.

I would like to do a cleaner that removes an element if stock and price equals the previous one, but only the previous one, excluding the date.

On this example, as third element equals the second, i'd like to remove it. If 4rth equals the 3rd, then remove it, and so on.

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
Mimetix
  • 272
  • 1
  • 4
  • 12
  • I think [this link](http://stackoverflow.com/questions/1068834/object-comparison-in-javascript) will be useful for you – KyawLay Sep 09 '14 at 08:45
  • possible duplicate of [Adding/removing items from JSON data with JQuery](http://stackoverflow.com/questions/4538269/adding-removing-items-from-json-data-with-jquery) – Luca Davanzo Sep 09 '14 at 08:46
  • @KyawLay mean [Link](http://stackoverflow.com/questions/1068834/object-comparison-in-javascript) he posted correct link but by mistake its become dead link.. try this link – Deepak Sharma Sep 09 '14 at 08:49
  • is demo I linked what you need? – Luca Davanzo Sep 09 '14 at 09:52

3 Answers3

0

DEMO

So, in this way you can iterate your objects. Than you can erase an element comparing with 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--;
    }
}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
0

You can do that easily 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);
    });
}
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
0

here is the raw 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 );
Faizy
  • 21
  • 7