how to manipulate an array of objects in javascript

I am trying to manipulate the object below i have given here i need to divide the object into multiple objects based on the qty and warehouse properties.

 var response =  [
              {
                "name": "test1",
                "qty": 3,    //divid obj to 3 times as qty haing 3 
                "warehouse": {
                  "india": 2,
                  "Usa": 1
                }
              },
              {
                "name": "test2",
                "qty": 1,
                "warehouse": {
                  "india": 1
                }
              },
              {
                "name": "test3",
                "qty": 1,
                "warehouse": {
                }
              } 
            ]

 // here i am trying to manipulate to get the result 
        const finalResponse = response.map(item=>{
              if((item.qty>=1) && (Object.keys(item.warehouse).length >= 1)){
                const warehouses = item.warehouse;
                const arr=[]
                Object.keys(warehouses).forEach((key)=>{
                  if(warehouses[key]>=1){
                    arr.push(...Array(warehouses[key]).fill({...item,[key]:1}))
                  }
                })
                return arr;
              }
            })

here i am trying to get output below but i am not able fine right solution

         finalResponse =[  {
                          "name": "test1",
                          "qty": 1,
                          "india": 1
                        },
                        {
                          "name": "test1",
                          "qty": 1,
                          "india": 1
                        },
                        {
                          "name": "test1",
                          "qty": 1,
                          "Usa": 1
                        },
                        {
                          "name": "test2",
                          "qty": 1,
                          "india": 1
                        },,
                     {
                        "name": "test3",
                        "qty": 1,
                        "warehouse": { 
                        }
                        } ];

Answer

You can easily achieve this result using flatMap, Object.keys, and Array.from

const response = [{
    name: "test1",
    qty: 3, //divid obj to 3 times as qty haing 3
    warehouse: {
      india: 2,
      Usa: 1,
    },
  },
  {
    name: "test2",
    qty: 1,
    warehouse: {
      india: 1,
    },
  },
];

const result = response.flatMap((o) => {
  const country = Object.keys(o.warehouse).flatMap((k) => Array(o.warehouse[k]).fill(k));
  
  return Array.from({ length: o.qty }, (_, i) => ({
    qty: 1,
    name: o.name,
    [country[i]]: 1,
  }));
});

console.log(result)
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

Edited: If flatMap is not supported then you can use reduce

const response = [
  {
    name: "test1",
    qty: 3, //divid obj to 3 times as qty haing 3
    warehouse: {
      india: 2,
      Usa: 1,
    },
  },
  {
    name: "test2",
    qty: 1,
    warehouse: {
      india: 1,
    },
  },
];

const result = response.reduce((acc, curr) => {
  const country = [];
  Object.keys(curr.warehouse).forEach((k) => {
    const result = Array(curr.warehouse[k]).fill(k);
    country.push(...result);
  });

  acc.push(
    ...Array.from({ length: curr.qty }, (_, i) => ({
      qty: 1,
      name: curr.name,
      [country[i]]: 1,
    }))
  );
  return acc;
}, []);

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

EDITED AGAIN If warehouse is empty then you could do as

var response = [
  {
    name: "test1",
    qty: 3,
    warehouse: {
      india: 2,
      Usa: 1,
    },
  },
  {
    name: "test2",
    qty: 1,
    warehouse: {
      india: 1,
    },
  },
  {
    name: "test3",
    qty: 1,
    warehouse: {},
  },
];

const result = response.reduce((acc, curr) => {
  const country = [];
  Object.keys(curr.warehouse).forEach((k) => {
    const result = Array(curr.warehouse[k]).fill(k);
    country.push(...result);
  });

  acc.push(
    ...Array.from({ length: curr.qty }, (_, i) => {
      const res = {
        qty: 1,
        name: curr.name,
      };
      if (country[i]) res[country[i]] = 1;
      else res.warehouse = {};
      return res;
    })
  );
  return acc;
}, []);

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }