jquery ajax每个函数检查返回ID与上一个相同

问题描述 投票:0回答:1

我正在使用AJAX从删除服务器中获取数据,然后使用每个循环。

现在我要做的是,如果返回值数据与上一个相同,我会做某事我有10个val.userId == 1,我只想做10次。

在实际情况下,我不知道要使其动态的用户ID,狐狸示例用户12123有10次,用户1239823有1000次

此处为示例https://jsonplaceholder.typicode.com/posts

$(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);

          }
    });
  }

感谢阅读

我正在使用AJAX从删除服务器中获取数据,然后使用每个循环。现在我想做的是,如果返回值数据与上一个相同,我将执行10 val.userId ...

javascript jquery json loops each
1个回答
0
投票

如果要基于userId删除重复的项目,则可以使用响应进行循环并过滤它们。

© www.soinside.com 2019 - 2024. All rights reserved.