Skip to content
Advertisement

How to remove last loop from foreach?

I grab json filtering with my own json. How to limit foreach for last loop?

JavaScript

Output :

JavaScript

Expected output :

JavaScript

The point is how to remove / exclude last “},{” in the end of loop?

Advertisement

Answer

Just store the total count of the result, and then if the output line is the last count then do not output the characters },{

So the codes (working) is:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement