Home > Mobile >  use foreach in this array in javascript
use foreach in this array in javascript

Time:05-05

the output is like this (its sort of date)

{"2022-04":[{"id":3,"price":"822.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\apple-watch.png","visit":"23","sourse_id":2,"published_at":"2022-04-06T02:22:07.000000Z","deleted_at":null,"created_at":"2022-05-05T00:20:33.000000Z","updated_at":"2022-05-05T02:22:20.000000Z"}],"2022-05":[{"id":2,"price":"500.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\Affidavit of Support I20 Request Form 2021-2022 Rates.pdf","visit":"47","sourse_id":2,"published_at":"2022-05-05T02:13:17.000000Z","deleted_at":null,"created_at":"2022-06-15T00:18:15.000000Z","updated_at":"2022-05-05T00:38:11.000000Z"}]}

now i want to foreach to this array

in php i use like this now in js what should i do?

   foreach ($ads as $key => $ad) {
        $day = $key;
        $totalCount = $ad->sum('visit');
    }

CodePudding user response:

You can use Object.entries() to make the object into an array and then iterate through it using any looping function you like.

const data = {"2022-04":[{"id":3,"price":"822.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\apple-watch.png","visit":"23","sourse_id":2,"published_at":"2022-04-06T02:22:07.000000Z","deleted_at":null,"created_at":"2022-05-05T00:20:33.000000Z","updated_at":"2022-05-05T02:22:20.000000Z"}],"2022-05":[{"id":2,"price":"500.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\Affidavit of Support I20 Request Form 2021-2022 Rates.pdf","visit":"47","sourse_id":2,"published_at":"2022-05-05T02:13:17.000000Z","deleted_at":null,"created_at":"2022-06-15T00:18:15.000000Z","updated_at":"2022-05-05T00:38:11.000000Z"}]}
Object.entries(data).forEach(d => {
  // Insert your code for each iteration here
   console.log(d);
});

CodePudding user response:

You could use for...in because you deal here object not array

let data = {"2022-04":[{"id":3,"price":"822.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\apple-watch.png","visit":"23","sourse_id":2,"published_at":"2022-04-06T02:22:07.000000Z","deleted_at":null,"created_at":"2022-05-05T00:20:33.000000Z","updated_at":"2022-05-05T02:22:20.000000Z"}],"2022-05":[{"id":2,"price":"500.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\Affidavit of Support I20 Request Form 2021-2022 Rates.pdf","visit":"47","sourse_id":2,"published_at":"2022-05-05T02:13:17.000000Z","deleted_at":null,"created_at":"2022-06-15T00:18:15.000000Z","updated_at":"2022-05-05T00:38:11.000000Z"}]};


for(let el in data){
    console.log(data[el])
}

Or you could use Object.values to get only the values like this

 let data = {"2022-04":[{"id":3,"price":"822.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\apple-watch.png","visit":"23","sourse_id":2,"published_at":"2022-04-06T02:22:07.000000Z","deleted_at":null,"created_at":"2022-05-05T00:20:33.000000Z","updated_at":"2022-05-05T02:22:20.000000Z"}],"2022-05":[{"id":2,"price":"500.000","receipt":"Uploads\\Advertise\\2022\\05\\05\\Affidavit of Support I20 Request Form 2021-2022 Rates.pdf","visit":"47","sourse_id":2,"published_at":"2022-05-05T02:13:17.000000Z","deleted_at":null,"created_at":"2022-06-15T00:18:15.000000Z","updated_at":"2022-05-05T00:38:11.000000Z"}]};
 
 Object.values(data).forEach(el => {
  console.log(el)
 })

  • Related