How to make a tour of N records in Javascript

1

I have this code that verifies me if the id number entered in a form exists in the registry:

callAjax("http://page.app/serial?" + Date["now"](), function (a) {
  console["log"]("resultcheck", a);
  try {
   a = JSON["parse"](a)
  } catch (e) {};
  g["freeBussy"] = false;
  // Aqui verrifico el id 
  if (b == a["id"]) {
   g["setstatus"](true, a["id"], f);
   return c(true)
  };
 g["setstatus"](false, null, f);
 return c(false)
}

The problem is that now I want to search and verify the id in all the records in the database.

Previously it sent an array in this way (Only 1 record):

{"id":2,"email":"[email protected]","name":"User example 1","online":0}

Now I'm sending this way, that is, N records.

[
  {"id":2,"email":"[email protected]","name":"User example 3","c":false},
  {"id":2,"email":"[email protected]","name":"User example 2","c":false},
  {"id":1,"email":"[email protected]","name":"User Example 1","c":false}
]

This is what I'm sending from the controller:

public function serial(Request $request){
    $users = User::select('id','email','name','online')->where('status','=','1')
                ->orderBy('id','DESC')
                ->get();

    return response()->json($users);
}

I thought of a foreach, but I was looking for google, and unfortunately I did not find much information about what I want to do.

    
asked by Jhosselin Giménez 21.05.2017 в 21:25
source

1 answer

1

Try forEach () . allows you to iterate and execute a function for each element of an array.

Example:

callAjax("http://page.app/serial?" + Date["now"](), function (a) {
  console["log"]("resultcheck", a);
  try {
   a = JSON["parse"](a)
  } catch (err) {console.log(err)};
  g["freeBussy"] = false;
  // Aqui verrifico el id 
  a.forEach(function(a) {
   if (b == a["id"]) {
    g["setstatus"](true, a["id"], f);
    return c(true)
   };
  });
  g["setstatus"](false, null, f);
  return c(false)
}
    
answered by 21.05.2017 в 23:40