0

I created an API where i want to get the return of some fields all together, I tried to concatenate them but it isn't giving me the result in a string.

export async function getByContaBancariaEmpresarialDescricao() {
  const resource = await Resource.query()
    .select('descricao', 'agencia', 'digito_agencia', 'conta', 
'digito_conta', 'ibi.codigo as bancoCodigo', 'ibi.numero as 
bancoNumero', 'ibi.banco as bancoNome', 'ibi.IMAGEM_LOGO as 
bancoICONE',
    ('descricao as ContaBancariaEmpresarialDescricao'),
    (`descricao`, '+', `conta`, '+', `ibi.numero`)
  )

    .joinRelation('banco', { alias: 'ibi' });


  return resource;
}

And it's returning this:

{
        "descricao": "BANCO TRIBANCO",
        "agencia": "1111",
        "digitoAgencia": "1",
        "conta": "1111",
        "digitoConta": "1",
        "bancoCodigo": 11,
        "bancoNumero": "111",
        "bancoNome": "BANCO TRIANGULO S/A",
        "bancoICONE": null,
        "ContaBancariaEmpresarialDescricao": "BANCO TRIBANCO",
        "numero": "111"
},

Is there a way to get a return of 'descricao + conta + bancoNumero' all together in a single string?

Akhil Aravind
  • 5,741
  • 16
  • 35
  • 1
    Can you please share the expected output? – Dipak Jul 10 '18 at 13:42
  • Or are you looking for something like this : https://stackoverflow.com/questions/8312459/iterate-through-object-properties – Dipak Jul 10 '18 at 13:44
  • The expected result would be: "ContaBancariaEmpresarialDescricao": "BANCO TRIBANCO, 1111, 111". Where ContaBancariaEmpresarialDescricao gives me the values of descricao + conta + bancoNumero. – Vicente Lima Lemes Jul 10 '18 at 13:45
  • 1
    @VicenteLimaLemes Use the [edit](https://stackoverflow.com/posts/51266799/edit) button please!!! – Samuel Hulla Jul 10 '18 at 13:46
  • @VicenteLimaLemes, i see the result and the expectation same ! – Basil Battikhi Jul 10 '18 at 13:47
  • The ContaBancariaEmpresarialDescricao is returning only 'descricao', and not all three fields together. – Vicente Lima Lemes Jul 10 '18 at 13:49
  • I think the problem is here: ('descricao as ContaBancariaEmpresarialDescricao'), (`descricao`, '+', `conta`, '+', `ibi.numero`) But I don't know how to change it to return all the three fields I want as ContaBancariaEmpresarialDescricao. – Vicente Lima Lemes Jul 10 '18 at 13:51

2 Answers2

0

Before this return resource; statement do the following to have the expected results as mentioned in the comment:

var newValue = [resource['ContaBancariaEmpresarialDescricao'], resource['conta'],resource['bancoNumero']].join(','); 
resource['ContaBancariaEmpresarialDescricao'] =newValue;
return resource;
Dipak
  • 6,532
  • 8
  • 63
  • 87
0

this is a JSON , you can parse this to JavaScript Object and the concatenate the fields, like this:

var obj = JSON.parse('{"descricao": "BANCO TRIBANCO","agencia": "1111","digitoAgencia": "1","conta": "1111","digitoConta": "1","bancoCodigo": 11,"bancoNumero": "111","bancoNome": "BANCO TRIANGULO S/A", "bancoICONE": null,"ContaBancariaEmpresarialDescricao": "BANCO TRIBANCO","numero": "111"}');

var result = obj.descricao +' ' + ' '+obj.conta + ' ' +obj.bancoNumero;
K.Martinez
  • 21
  • 4