Javascript function to return an array that needs to be in a specific order, depending on the order of a different array
I would

Encapsulate the names and colors in a data structure
Use .map instead of .forEach

So something like this;
function chartBackgroundColor(collection){
  const nameColorMap = {
    &quot;TripAdvisor&quot;: &quot;#00B98B&quot;,
    &quot;Google&quot;: &quot;#FFBD00&quot;,
    &quot;Facebook&quot;: &quot;#4B6DAA&quot;,
  };
  return collection.map(data =&gt; nameColorMap[data.name] || &quot;#D9D9D9&quot;);
}