There are 5 Balls with 3 different color You Need to Put it in their Respective Color Bucket.

There are 5 Balls with 3 different color You Need to Put it in their Respective Color Bucket.

Interview Question

var balls = [
  {
    name: "Ball1",
    color: "RED",
  },
  {
    name: "Ball2",
    color: "BLUE",
  },
  {
    name: "Ball3",
    color: "BLUE",
  },
  {
    name: "Ball4",
    color: "GREEN",
  },
  {
    name: "Ball5",
    color: "GREEN",
  },
];
var Green_BALL = [];
var Blue_BALL = [];
var Red_BALL = [];
balls.map((data) => {
  if (data.color == "GREEN") {
    Green_BALL.push(data.name);
  } else if (data.color == "BLUE") {
    Blue_BALL.push(data.name);
  } else {
    Red_BALL.push(data.name);
  }
});
console.log("GREEN_BALLS=>", Green_BALL);
console.log("Red_BALLS=>", Red_BALL);
console.log("Blue_BALLS=>", Blue_BALL);
Output:
GREEN_BALLS=> [ 'Ball4', 'Ball5' ]
Red_BALLS=> [ 'Ball1' ]
Blue_BALLS=> [ 'Ball2', 'Ball3' ]