I'm trying clean/filter a array to download in CSV, but I have a trouble to make this work... I make this for to catch bigger array to make new array with old array.
oldArray:
[
{
Cpf: null,
Nascimento: null,
Sexo: null,
OnlyPerson: false,
IsFinanc: false,
Senha: null,
ConfirmaSenha: null,
Remover: false,
TipoStr: null,
FiltroStr: null,
IdAgenciaLogarComo: 0,
DontHashPass: false,
IsPessoaSimples: false,
IsVisitante: false,
Permited: false,
Id: 21980,
Nome: 'arrozfeijao',
Ativo: true,
Criacao: '2021-08-19T14:09:06.173',
UltimaAlteracao: null,
Email: '[email protected]',
IdAgencia: 1,
IdEndereco: null,
IdPermissao: 4,
Observacoes: null,
Endereco: {
Cep: null,
Logradouro: null,
Numero: null,
Complemento: null,
Bairro: null,
Estado: null,
Cidade: null,
},
Parceiro: null,
Contato: [],
Permissao: {
Id: 4,
Descricao: 'Cliente',
Pessoa: [],
},
AlterarSenha: [],
Rede: [],
Provider: [],
AlertaPreco: [],
Pedido2: [],
_PageNumber: 0,
PageNumber: 0,
PageSize: 0,
OrderBy: null,
OrderDesc: false,
},
];
Function to clean array:
for (const [key] of Object.entries(this.oldArray)) {
let tempObject = {};
for (const [keys, values] of Object.entries(this.oldArray[key])) {
if (this.includesArray.includes(keys)) {
tempObject[keys] = values;
}
}
this.newArray[key] = tempObject;
}
works fine, I put in "includesArray" just I need to return
(ex. includesArray: ["Cpf", "Nascimento", "Sexo", "Id", "Nome", "Ativo", "Criacao", "UltimaAlteracao", "Email", "Observacoes", "Endereco"])
But -> "Endereco" is another array! if I display "newArray" it shows me that:
[
{
"Cpf": null,
"Nascimento": null,
"Sexo": null,
"Id": 21980,
"Nome": "arrozfeijao",
"Ativo": true,
"Criacao": "2021-08-19T14:09:06.173",
"UltimaAlteracao": "2021-08-19T14:09:06.173",
"Email": "[email protected]",
"Observacoes": null,
"Endereco": {
"Id": 0,
"Cep": null,
"Logradouro": null,
"Numero": null,
"Complemento": null,
"Bairro": null,
"Estado": null,
"Cidade": null
}
}
]
I need make this happen to this array:
"Cpf": null,
"Nascimento": null,
"Sexo": null,
"Id": 21980,
"Nome": "arrozfeijao",
"Ativo": true,
"Criacao": "2021-08-19T14:09:06.173",
"UltimaAlteracao": "2021-08-19T14:09:06.173",
"Email": "[email protected]",
"Observacoes": null,
"Cep": null,
"Logradouro": null,
"Numero": null,
"Complemento": null,
"Bairro": null,
"Estado": null,
"Cidade": null
I need delete? i need pop()? I need Splice? and push again? I really don't know what to do...
CodePudding user response:
You can try this:
const arr = [
{
"Cpf": null,
"Nascimento": null,
"Sexo": null,
"Id": 21980,
"Nome": "arrozfeijao",
"Ativo": true,
"Criacao": "2021-08-19T14:09:06.173",
"UltimaAlteracao": "2021-08-19T14:09:06.173",
"Email": "[email protected]",
"Observacoes": null,
"Endereco": {
"Id": 0,
"Cep": null,
"Logradouro": null,
"Numero": null,
"Complemento": null,
"Bairro": null,
"Estado": null,
"Cidade": null
}
}
]
const res = {}
const obj = arr[0]
for(const el in obj) {
if(el === 'Endereco') {
for(const e in obj[el]) {
if (e !== 'Id') res[e] = el[e] || null
}
} else {
res[el] = obj[el]
}
}
console.log(res)
CodePudding user response:
Since you're already declaring an includes
array, you may as well define a constructor for your result object.
Here destructuring the properties from your includedArray
from each passed object, further destructruing the Endereco
property to isolate the unwanted Id
and assigning the remaining properties to a parameter to later be spread into the final object.
const input = [ { Cpf: null, Nascimento: null, Sexo: null, OnlyPerson: false, IsFinanc: false, Senha: null, ConfirmaSenha: null, Remover: false, TipoStr: null, FiltroStr: null, IdAgenciaLogarComo: 0, DontHashPass: false, IsPessoaSimples: false, IsVisitante: false, Permited: false, Id: 21980, Nome: 'arrozfeijao', Ativo: true, Criacao: '2021-08-19T14:09:06.173', UltimaAlteracao: null, Email: '[email protected]', IdAgencia: 1, IdEndereco: null, IdPermissao: 4, Observacoes: null, Endereco: { Id: 0, Cep: null, Logradouro: null, Numero: null, Complemento: null, Bairro: null, Estado: null, Cidade: null, }, Parceiro: null, Contato: [], Permissao: { Id: 4, Descricao: 'Cliente', Pessoa: [], }, AlterarSenha: [], Rede: [], Provider: [], AlertaPreco: [], Pedido2: [], _PageNumber: 0, PageNumber: 0, PageSize: 0, OrderBy: null, OrderDesc: false, }, ];
const ResultObject = ({
Cpf,
Nascimento,
Sexo,
Id,
Nome,
Ativo,
Criacao,
UltimaAlteracao,
Email,
Observacoes,
Endereco: { Id: _Id, ..._Endereco }, /* destructure the Id and '...rest' */
}) => ({
Cpf,
Nascimento,
Sexo,
Id,
Nome,
Ativo,
Criacao,
UltimaAlteracao,
Email,
Observacoes,
..._Endereco, /* spread the properties of the 'Endereco' object */
});
const result = input.map(ResultObject);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }