【问题标题】:delete specific array in array of objects with javascript使用javascript删除对象数组中的特定数组
【发布时间】:2021-11-11 15:26:03
【问题描述】:

我正在尝试清理/过滤要以 CSV 格式下载的数组,但我无法完成这项工作... 我这样做是为了捕捉更大的数组以用旧数组制作新数组。

旧数组:

[
  {
    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: 'arrozfeijao@gmail.com',
    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] 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;
}

工作正常,我输入“includesArray”只是我需要返回

(ex. includesArray: ["Cpf", "Nascimento", "Sexo", "Id", "Nome", "Ativo", "Criacao", "UltimaAlteracao", "Email", "Observacoes", "Endereco"])

但是 -> "Endereco" 是另一个数组! 如果我显示 “newArray” 它告诉我:

[
 {
  "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": "arrozfeijao@gmail.com",
  "Observacoes": null,
  "Endereco": {
    "Id": 0,
    "Cep": null,
    "Logradouro": null,
    "Numero": null,
    "Complemento": null,
    "Bairro": null,
    "Estado": null,
    "Cidade": null
   }
  }
 ]

我需要让这个数组发生这种情况:

  "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": "arrozfeijao@gmail.com",
  "Observacoes": null,
  "Cep": null,
  "Logradouro": null,
  "Numero": null,
  "Complemento": null,
  "Bairro": null,
  "Estado": null,
  "Cidade": null

我需要删除吗?我需要流行音乐()?我需要拼接?然后再推?我真的不知道该怎么办......

【问题讨论】:

  • 在同一个对象中有两个 Id 键是没有意义的。这是故意的吗?
  • 其实它不应该存在2个Id
  • 那么,您能否更新帖子以显示没有两个 ID 的正确输出?
  • 完成!我删除了 ID 并发布了 oldArray!

标签: javascript arrays vue.js vuejs2 vue-component


【解决方案1】:

由于您已经声明了一个includes 数组,您不妨为结果对象定义一个构造函数。

这里destructuring 来自每个传递的对象的includedArray 的属性,进一步解构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', UltimaAlteracao: null, Email: 'arrozfeijao@gmail.com', 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; }

【讨论】:

    【解决方案2】:

    你可以试试这个:

    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": "arrozfeijao@gmail.com",
      "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)

    【讨论】:

    • 它不能正常工作,你需要记住它是一个表格,每个复选框都包含一个新数组:c
    猜你喜欢
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多