Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较对象中的元素_Javascript_Json_Object - Fatal编程技术网

Javascript 比较对象中的元素

Javascript 比较对象中的元素,javascript,json,object,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

我正在使用一个将结果保存到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"}]}
 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 );
因此,结果的第一个要素是:

{"Stock":1,"Price":11.4,"Fecha":"18-8-2014:3:36"} 
 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 );
第二个是

{"Stock":1,"Price":12.4,"Fecha":"18-8-2014:3:38"} 
{"Stock":1,"Price":12.4,"Fecha":"19-8-2014:0:40"} and so.
 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 );
第三个是

{"Stock":1,"Price":12.4,"Fecha":"18-8-2014:3:38"} 
{"Stock":1,"Price":12.4,"Fecha":"19-8-2014:0:40"} and so.
 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 );
每次我浏览网页时,它都会添加一个元素

 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 );
我想做一个清洁剂,删除一个元素,如果股票和价格等于前一个,但只有前一个,不包括日期

 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 );
在这个例子中,由于第三个元素等于第二个元素,我想删除它。如果4rth等于第三个,则删除它,依此类推。

 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 );
因此,通过这种方式,您可以迭代对象。然后您可以将一个元素与另一个元素进行比较:

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--;
    }
}
 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 );
var arr={“Productos”:[
{“标题”:“Grabador de voz ISD1932”,“结果”:[
{“股票”:1,“价格”:11.4,“Fecha”:“18-8-2014:3:36”},
{“股票”:1,“价格”:12.4,“Fecha”:“18-8-2014:3:38”},
{“股票”:1,“价格”:12.4,“Fecha”:“19-8-2014:0:40”},
{“股票”:1,“价格”:12.4,“Fecha”:“19-8-2014:21:46”},
{“股票”:1,“价格”:12.4,“Fecha”:“21-8-2014:22:4”},
{“股票”:1,“价格”:12.4,“Fecha”:“22-8-2014:0:40”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:0:48”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
{“股票”:1,“价格”:12.4,“Fecha”:“23-8-2014:13:56”},
“id”:“4a1e90d7-e578-4bd5-b888-38c7bbfb4af5”}];
var data=arr.Productos[0]。结果;
for(var i=0;i!=data.length;i++){
var stock=数据[i]。股票;
var价格=数据[i]。价格;
var fecha=数据[i].fecha;
if(i
您可以使用
数组轻松完成此操作。筛选器

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);
    });
}
 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 );
var数据=。。。;
对于(var i=0;i
以下是使用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 );
var jsonObj={“结果”:{}
var结果={};
var tempArr=[];
对于(var i=0;i<10;i++){
var股票=i+20;
var price=“14”。+(i+10);
var fetch='12'+i
结果={'stock':stock'price':price'fetch':fetch};
如果(i==0)tempArr.push(结果);
否则{
var lastObj=tempArr[tempArr.length-1];
如果(lastObj.stock==stock&&lastObj.price==price){
console.log(“找到匹配项”);//如果找到匹配项,则不添加
}  
else tempArr.push(结果);
}  
}  
jsonObj.result=tempArr;
stringify(jsonObj);

我认为@KyawLay的可能副本对您很有用,因为他发布了正确的链接,但由于错误,它变成了死链接。。试试这个链接是我链接了你需要的吗?这并没有回答问题。显然是的(: