Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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 jqueryajax每个函数的检查返回ID都与前一个相同_Javascript_Jquery_Json_Loops_Each - Fatal编程技术网

Javascript jqueryajax每个函数的检查返回ID都与前一个相同

Javascript jqueryajax每个函数的检查返回ID都与前一个相同,javascript,jquery,json,loops,each,Javascript,Jquery,Json,Loops,Each,我使用AJAX从移除服务器获取数据,然后使用每个服务器循环 现在我想做的是,如果返回值数据与前一个相同,我会做一些事情 我有10个val.userId==1,我只想做一次而不是10次 在真实的情况下,我不知道用户ID,我想让它成为动态的,怎么样 福克斯示例用户12123有10次,用户1239823有1000次 举个例子 感谢您阅读如果您想删除基于用户ID的重复项,您可以对响应进行循环并对其进行过滤 $(文档).ready(函数(){ getData(); }); 函数getData(){ $

我使用AJAX从移除服务器获取数据,然后使用每个服务器循环

现在我想做的是,如果返回值数据与前一个相同,我会做一些事情 我有10个val.userId==1,我只想做一次而不是10次

在真实的情况下,我不知道用户ID,我想让它成为动态的,怎么样 福克斯示例用户12123有10次,用户1239823有1000次

举个例子


感谢您阅读

如果您想删除基于
用户ID的重复项
,您可以对响应进行循环并对其进行过滤

$(文档).ready(函数(){
getData();
});
函数getData(){
$.ajax({
键入:“获取”,
url:“https://jsonplaceholder.typicode.com/posts",
数据类型:“json”,
成功:功能(响应){
var-arr=[];
响应。forEach(功能(项目){
var result=arr.find(x=>x.userId==item.userId);
如果(!结果){
arr.push(项目);
}
});
控制台日志(arr);
},
错误:函数(xhr,textStatus,error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(错误);
}
});
}

如果要基于
用户ID删除重复项,可以使用响应进行循环并过滤它们

$(文档).ready(函数(){
getData();
});
函数getData(){
$.ajax({
键入:“获取”,
url:“https://jsonplaceholder.typicode.com/posts",
数据类型:“json”,
成功:功能(响应){
var-arr=[];
响应。forEach(功能(项目){
var result=arr.find(x=>x.userId==item.userId);
如果(!结果){
arr.push(项目);
}
});
控制台日志(arr);
},
错误:函数(xhr,textStatus,error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(错误);
}
});
}
$(document).ready(function(){
        getData();
      });


function getData(){
    $.ajax({
          type: "GET",
          url: "https://jsonplaceholder.typicode.com/posts",
          dataType: 'json',
          success: function(response){

              $.each(response, function(index, val) {
                console.log(val); // get all return data
                // as we can see userId == 1 have 10 posts , I just want to console.log only once if usdId == 1
                if(val.userId == 1){
                  console.log(val.userId);  // this one consoloe .log 10 times since we have 10 userId 
                }
                // how about in the real case I do not know the user ID i wanna make it dynamic
                // fox example user 12123  has 10 times ,  user 1239823 has 1000 times 

              });


          },
          error: function(xhr, textStatus, error){console.log(xhr.statusText);console.log(textStatus);console.log(error);

          }
    });
  }