Home > OS >  how to convert an array of objects into single object
how to convert an array of objects into single object

Time:12-12

I am Getting response data from node.js api in below json format where i am joining two tables with inner join . table 1 is having columns: id, name, description, layername and table 2 is having columns attrName, datatype. one to many relationship between two tables.

   [
{
    "id": 788,
    "name": "ABC",
    "description": "abc",
    "layername": "abclayer",
    "file_type": "Parquet",
    "attrname": "Id",
    "datatype": "integer",
    "length": 20
},
{
    "id": 788,
    "name": "ABC",
    "description": "abc",
    "layername": "abclayer",
    "file_type": "Parquet",
    "attrname": "Name",
    "datatype": "varchar",
    "length": 40
    
}

]

But I need response data in below format. What is the correct way to get data by writing query for both tables.

{
"name": "ABC",
"description": "abc",
"fieldDetails": [
    {
        "attributeName": "Id",
        "datatype": "integer",
        "length": 20
    },   {
        "attributeName": "Name",
        "datatype": "varchar",
        "length": 40
    },    
],
"layername": "abclayer",
"file_type": "Parquet"
}

node.js api code.

    getDataById = (dP, id) => {
    return new Promise((resolve, reject) => {
        const userDBName = dP.DatabaseName   ".\""   dP.SchamaName   "\"";
        const dimTable = dP.DimensionListTable;
        const dimConfigTable = dP.DimensionConfigTable;

        let query = "Select dim.id, dim.name, dim.description, dim.layername, dim.file_type, dimConfig.attributename, "  
        "dimConfig.datatype, dimConfig.length, dimConfig.primarykey, dimConfig.partitionby, dimConfig.start_index "  
        "From "   userDBName   "."   dimTable   " dim INNER JOIN "   userDBName   "."   dimConfigTable   " dimConfig "  
        "ON dim.id = dimConfig.dim_id  and dim.Id = '"   id   "'";

        console.log("newwwwQueryyy "   query)
        mysql.executeQuery(query).then(result => {
            console.log("schemaData"   JSON.stringify(result)   "count"   result.length);
            resolve({ "schemaData": result, "count": result.length });

        }).catch(err => {
            console.log(resolve({ "schemaData": [], "count": 0 }));
        })
    });
}

CodePudding user response:

You can use reduce.

Stackblitz code

const data =   [
    {
        "id": 788,
        "name": "ABC",
        "description": "abc",
        "layername": "abclayer",
        "file_type": "Parquet",
        "attrname": "Id",
        "datatype": "integer",
        "length": 20
    },
    {
        "id": 788,
        "name": "ABC",
        "description": "abc",
        "layername": "abclayer",
        "file_type": "Parquet",
        "attrname": "Name",
        "datatype": "varchar",
        "length": 40
        
    }
]  

const obj = data.reduce((acc, curr) => {
    return {
        ...acc,
        fieldDetails: [...acc.fieldDetails, {
            attributeName: curr.attrname,
            datatype: curr.datatype,
            length: curr.length
        }]
    }
}, {
    name: data[0].name,
    description: data[0].description,
    layername: data[0].layername,
    file_type: data[0].file_type,
    fieldDetails: []
});

console.log(obj);

CodePudding user response:

const data = [
    {
        "id": 788,
        "name": "ABC",
        "description": "abc",
        "layername": "abclayer",
        "file_type": "Parquet",
        "attrname": "Id",
        "datatype": "integer",
        "length": 20
    },
    {
        "id": 788,
        "name": "ABC",
        "description": "abc",
        "layername": "abclayer",
        "file_type": "Parquet",
        "attrname": "Name",
        "datatype": "varchar",
        "length": 40

    }
]

let fieldDetails = [];

data.forEach(element => {
    fieldDetails.push({
        "attributeName": element['attrname'],
        "datatype": element['datatype'],
        "length": element['length']
    })
});

const newdata = data.map(x => {
    return {
        "name": x.name,
        "description": x.description,
        "fieldDetails": fieldDetails,
        "layername": x.layername,
        "file_type": x.file_type
    }
})

console.log(newdata[0])

  • Related