The algorithm you're looking for is called A* Search Algorithm. It is a best-first search algorithm that works by beginning at the starting point and building up a series of possible paths (excluding going through obstacles since those aren't possible paths), then scoring those paths to find the least cost one. In your case you'd need to customize the scoring by decreasing the cost based upon the objects along the path and increasing the cost by distance.
There's some information that will help you with it here:
There's a nifty interactive demo here (code also on github): http://qiao.github.io/PathFinding.js/visual/
Other resources: