我正在嘗試清理/過濾一個陣列以下載到CSV中,但我有一個麻煩,就是要使這個作業正常。 我這樣做是為了捕捉更大的陣列,使新的陣列與舊的陣列結合起來。
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,
},
];
清理陣列的函式:
for (const [key] ofObject. entries(this.oldArray) {
let tempObject = {};
for (const [keys, values] of Object. entries(this.oldArray[key]) {
if (this.includesArray.includes(key)) {
tempObject[keys] = values;
}
}
this.newArray[key] = tempObject;
}
作業正常,我輸入了 "includesArray",只是我需要回傳
。(ex. includesArray: ["Cpf"/span>, "Nascimento"/span>, "Sexo"/span>, "Id", "Nome", "Ativo", "Criacao", "UltimaAlteracao", "Email", "Observacoes", "Endereco"] )
但是 -> "Endereco "是另一個陣列! 如果我顯示 "newArray",它顯示的是:
[
{
"Cpf"/span>。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, "Cidade".
}
}
]
我需要讓這個陣列發生這種情況:
我需要讓這個陣列發生這種情況。
"Cpf"/span>: 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.
我需要洗掉?我需要pop()?我需要Splice? 和push again? 我真的不知道該怎么做......
uj5u.com熱心網友回復:
你可以試試這個:
。const arr = [
{
"Cpf"/span>: 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, "Cidade".
}
}
]
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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
既然你已經宣告了一個includes陣列,你也可以為你的結果物件定義一個建構式。
這里解構你的includesArray中的屬性,從每個傳遞的物件中,進一步解構Endereco屬性以隔離不需要的Id,并將剩余的屬性分配給一個引數,以便以后spread到最終物件中。
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'/span>, 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,
壽命。
Sexo,
Id,
名稱。
興趣。
罪行。
最終結果。
電子郵件。
觀察者。
稅務局: { Id: _Id, ..._Endereco }, /* destructure the Id and '...rest' */.
}) => ({
Cpf,
Nascimento,
Sexo。
Id,
Nome,
Ativo。
Criacao。
UltimaAlteracao,
Email,
Observacoes,
..._Endereco, /*傳播'Endereco'物件的屬性 */.
});
const result = input.map(ResultObject)。
console.log(result);
.as-console-wrapper { max-height。100% ! important; top: 0; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/310005.html
標籤:
