Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
How can i dynamicly map column names on import

Here is my situation, i am importing from multiple data sources into my json backend.

The mapping file will look something like this sample below. So the question is how could i go about it to effectively look up the mapping on an import from this json file and use it when to import the data into my backend. So the concept is to pass 2 objects to my function, the mapping file and the json object which holds the data from the vendor. Then i have to use the mapping and loop thru data to create an object / doc and insert in my backend with correct mappings.

{
    "vendor" : "ABCVendor",
    "version" : "2.01",
    "mappings" : [
        {   "fieldName" : "fName",
            "dbName" : "FirstName",
            "required" : true,
            "type" : "string"
        },
                {   "fieldName" : "mName",
            "dbName" : "MiddleName",
            "required" : false,
            "type" : "string"
        },
                {   "fieldName" : "lName",
            "dbName" : "LastName",
            "required" : true,
            "type" : "string"
        }
    
    ]
}

Here is what the input would look like

{
        "fname" : "Steve",
        "mname" : "T",
        "lname" : "Miller"
}

Ad here is what i would like to get out

{
        "FirstName" : "Steve",
        "MiddleName" : "T",
        "LastName" : "Miller"
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could do something like this, if the insert is always the same.

this may not be the most performant way to do it though.

"use strict";
const fileConfig = {
    "vendor": "ABCVendor",
    "version": "2.01",
    "mappings": [
        {
            "fieldName": "fName",
            "dbName": "FirstName",
            "required": true,
            "type": "string"
        },
        {
            "fieldName": "mName",
            "dbName": "MiddleName",
            "required": false,
            "type": "string"
        },
        {
            "fieldName": "lName",
            "dbName": "LastName",
            "required": true,
            "type": "string"
        }
    ]
};
const funcInput = {
    "fName": "Steve",
    "mName": "T",
    "lName": "Miller"
};
function change(config, input) {
    const result = {};
    config.mappings.forEach((fe) => {
        result[fe.dbName] = input[fe.fieldName];
    });
    return result;
}

console.log(change(fileConfig, funcInput))

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do this all with reduce (although there does appear to be a casing difference in your mapping vs your object which ive used toLowerCase to get aeround - maybe uunncessary if that was just a typo on your part)

const config = {
    "vendor" : "ABCVendor",
    "version" : "2.01",
    "mappings" : [
        {   "fieldName" : "fName",
            "dbName" : "FirstName",
            "required" : true,
            "type" : "string"
        },
                {   "fieldName" : "mName",
            "dbName" : "MiddleName",
            "required" : false,
            "type" : "string"
        },
                {   "fieldName" : "lName",
            "dbName" : "LastName",
            "required" : true,
            "type" : "string"
        }
    
    ]
}
const input = {
        "fname" : "Steve",
        "mname" : "T",
        "lname" : "Miller"
}

const result = config.mappings.reduce( 
        (acc, {fieldName, dbName}) => ({...acc, [dbName]: input[fieldName.toLowerCase()]})
    ,{})

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda