Home > OS >  Make a single array from multiple object in same value
Make a single array from multiple object in same value

Time:06-08

I am facing a problem in express js. I have 2 object which data comes from DB for the same product code in different product sale. After retrieve the data, I want to make an object /array which will be a single array. My Sample Object which comes from db is like

RowDataPacket {
        region: 'Rajshahi',
        area: 'Bogra',
        house: 'Mr Brothers',
        territory: 'Bogra 1',
        point: 'Bogura',
        skid: 20000007,
        dpid: 221,
        route: '18A',
        retailer_code: 'RAJ-38-00526',
        volume: 4,
        value: 5840,
        date: 2022-01-30T18:00:00.000Z
    }
  
  RowDataPacket {
        region: 'Rajshahi',
        area: 'Bogra',
        house: 'Mr Brothers',
        territory: 'Bogra 1',
        point: 'Bogura',
        skid: 20000003,
        dpid: 221,
        route: '18A',
        retailer_code: 'RAJ-38-00526',
        volume: 5,
        value: 8952,
        date: 2022-01-30T18:00:00.000Z
    }

And the output which I wish like

{
        region: 'Rajshahi',
        area: 'Bogra',
        house: 'Mr Brothers',
        territory: 'Bogra 1',
        point: 'Bogura',
        dpid: 221,
        route: '18A',
        retailer_code: 'RAJ-38-00526',
        date: 2022-01-31
        sale_info :{
                skid:20000003
                volume:5,
                value:8952
              },
              { 
                skid:20000007
                volume:4,
                value:5840
              }
      }

I have tried by the below code which doesnt work. Please help me. Here skid refers the product code.Thanks in advance.

var final_arr = []; 
    preorder_data.forEach(async (val, key) => {
        var temp_arr = [];         
           
        temp_arr.push(val.region);
        temp_arr.push(val.area);
        temp_arr.push(val.house);
        temp_arr.push(val.territory);
        temp_arr.push(val.point);
        temp_arr.push(val.route);
        temp_arr.push(val.retailer_code);       
      
        final_arr.push(temp_arr);
    });

CodePudding user response:

Here's one way to do it:

// Initial data
const preorder_data = [
{
    region: 'Rajshahi',
    area: 'Bogra',
    house: 'Mr Brothers',
    territory: 'Bogra 1',
    point: 'Bogura',
    skid: 20000007,
    dpid: 221,
    route: '18A',
    retailer_code: 'RAJ-38-00526',
    volume: 4,
    value: 5840,
    date: '2022-01-30T18:00:00.000Z'
},
{
    region: 'Rajshahi',
    area: 'Bogra',
    house: 'Mr Brothers',
    territory: 'Bogra 1',
    point: 'Bogura',
    skid: 20000003,
    dpid: 221,
    route: '18A',
    retailer_code: 'RAJ-38-00526',
    volume: 5,
    value: 8952,
    date: '2022-01-30T18:00:00.000Z'
}];

// Get the "sales data" from each object in initial data array (skid, value, volume) and place it into a sales_data array
const sales_data = preorder_data.map((x) =>
{
    const { skid, value, volume } = x;
    return { skid, value, volume };
});

// Make the final, single object, which has the sales_data array
// We just grab all the keys of the first object in the initial data array, 
// filter out the sales related keys and attach the sales data to it
const final = { ...Object.fromEntries(Object.entries(preorder_data[0]).filter(([key]) => 
{
    return !key.includes('skid') && !key.includes('value') && !key.includes('volume')
})), sales_data };

CodePudding user response:

fist create array of your data like this

const preorder_data = [
  {
    region: "Rajshahi",
    area: "Bogra",
    house: "Mr Brothers",
    territory: "Bogra 1",
    point: "Bogura",
    skid: 20000007,
    dpid: 221,
    route: "18A",
    retailer_code: "RAJ-38-00526",
    volume: 4,
    value: 5840,
    date: "2022-01-30T18:00:00.000Z",
  },
  {
    region: "Rajshahi",
    area: "Bogra",
    house: "Mr Brothers",
    territory: "Bogra 1",
    point: "Bogura",
    skid: 20000003,
    dpid: 221,
    route: "18A",
    retailer_code: "RAJ-38-00526",
    volume: 5,
    value: 8952,
    date: "2022-01-30T18:00:00.000Z",
  },
];

then do like this

let dataArr = {};
let newData = preorder_data.map((data) => {
  dataArr = {
    ...dataArr,
    region: data.region,
    area: data.area,
    house: data.house,
    territory: data.territory,
    point: data.point,
    dpid: data.dpid,
    route: data.route,
    retailer_code: data.retailer_code,
    date: data.date,
  };
  return { skid: data.skid, value: data.value, volume: data.volume };
});
dataArr = { ...dataArr, newData };
  • Related