Changing strings of an object in a foreach

Asked

Viewed 40 times

-3

I’m in trouble: I mount an object from an insert list:

1° imagem - objeto montado dinamicamente

This list I put in a variable and insert inside a function with foreach, however, so that it works correctly within the function can not have quotes (") in the records; The correct is to stay as follows:

2° imagem - como deve ficar, vindo da variável dinâmica

Thus, with the quotes in each record, the final result does not have the data of the Array that is made the function of the foreach, but as written in the first image, it is printed as: ["Bairro: row.bairro"] because of the quotes. I need to make it look like in the second image.

Return of the Associated variable:

array - associated

Function without being dynamic:

função completa

Function mounting the object:

função que monto o objeto

List for dynamic choice of items:

lista para escolha dos itens

  • the guys put negative and didn’t even understand the function, negative us without knowing, instead of helping, can only be beginner...

  • You could give an example how is your variable "this.Associated"?

  • 1

    Please click on [Edit] and put the code as text. Putting it as an image is not ideal, understand the reasons reading the FAQ.

  • @Brunocunha, the question is not even the return of this array, because if I do it manually as in 2° image, I can print it correctly, the question is the object that is mounted (dynamically) to choose the items to be printed. So at the end I have an object with the chosen items. However, when I have to join the foreach function, the quotation mark disturbs the function does not print the correct value, the right would be to remove the quotation marks and look like in 2° image

  • @Henriquemendessilveirarodri, as is its foreach function that is creating the object with the quotation marks, it disappeared from the question?

  • @Brunocunha, I do not create the object with the foreach function, the object is a part of the foreach function. is a normal object that I create through a list of options, I will cycle as the user selects and at the end I mount an object, as in the 1° image

  • @Brunocunha, I put the complete function that generates excel, but it is not dynamic. I want to leave dynamics in the object that composes the line 155 to 175. It’s as if this was hardcode, and I want to leave dynamic, passing an object with the same pattern, but I need to assemble this object without the quotes wrapping each record

  • Okay, I’m starting to understand, could you just put tbm as is your dynamic function?

  • @Brunocunha, I put the function that assembles the object, leaving the dynamics, as well as the list also that is shown. From this list, I just take the last part [Row] as you can see in the function that I mount the object with spread Operator on line 107

  • 1

    Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

  • @Bacco, thanks for the suggestion, but I edited more than three times talking to Bruno, to have a better understanding, what I intend to do is really complicated, but if you read the whole question I believe it is understandable my problem, in the end it is only about how to take the quotation marks from the records of the object of 1° image

Show 6 more comments

1 answer

1

From what I understand you want to mount a dynamic object with attributes that are marked with checked: true in your itemsList.

Your end result is getting "Neighborhood: Row.neighborhood" as you are including only strings in your array.

One solution I found would be to filter only the items that are with checked: true, and then iterate over your array associated creating new objects only with the attributes you want.

const itemsList = [
 {"id": 1, "name": "bairro", "checked": true, "row": "bairro", "title": "Bairro"},
 {"id": 2, "name": "celular", "checked": false, "row": "celular", "title": "Celular"},
 {"id": 3, "name": "cep", "checked": true, "row": "cep", "title": "Cep"},
];

const associated = [
  {"bairro": 'PARAISO', 'celular': "11111", 'cep': '29144-254'},
  {"bairro": 'OSASCO', 'celular': "22222", 'cep': '22455-774'},
];

// filtro apenas os itemsList que estão como true
const listRowsIterable = itemsList.filter(item => item.checked == true);

//monto objeto dinamicamente apenas com os atributos que quero 
const excel = associated.map(row => {
  let rowExcel = {};
  listRowsIterable.forEach(item => {
    //item.title - adiciona um atributo dinâmico no seu novo objeto
    //item.row - pega o valor que está no seu objeto associated dinâmicamente 
    rowExcel[item.title] = row[item.row];
  });
  return rowExcel;
});

console.log(excel);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.