0

i want to copy some elements from an object to another object

I have this :

var arrLinks = [
              {
                titulo: "CUADERNO DE BONOS SEGUROS BX+ 2020",
                fecha: "2020-02-18 11:02:41.0",
                categoria: "BONOS E INCENTIVOS",
                descripcion: "Cuaderno de Bonos e Incentivos"               
              },
              {
                titulo: "ACTUALIZACIÓN TARIFA AUTOS / PICKUP 2019",
                fecha: "2019-07-22 10:57:43.0",
                categoria: "CIRCULARES",
                descripcion: "Actualización de tarifa para Seguro de Auto B×+ y Seguro de Pick Up B×+ 2019"             
              },
              {
                titulo: "AVISO DE ACCIDENTES O ENFERMEDAD - GMM",
                fecha: "2019-09-18 12:42:45.0",
                categoria: "SINIESTROS PERSONAS",
                descripcion: "Aviso de Accidentes o Enfermedad Gastos Médicos Mayores"              
              }           
            ];

I want to have this:

var arrLinks2 = [
          {
            titulo: "CUADERNO DE BONOS SEGUROS BX+ 2020",               
            categoria: "BONOS E INCENTIVOS",
            descripcion: "Cuaderno de Bonos e Incentivos"               
          },
          {
            titulo: "ACTUALIZACIÓN TARIFA AUTOS / PICKUP 2019",             
            categoria: "CIRCULARES",
            descripcion: "Actualización de tarifa para Seguro de Auto B×+ y Seguro de Pick Up B×+ 2019"             
          },
          {
            titulo: "AVISO DE ACCIDENTES O ENFERMEDAD - GMM",               
            categoria: "SINIESTROS PERSONAS",
            descripcion: "Aviso de Accidentes o Enfermedad Gastos Médicos Mayores"              
          }           
        ];

Copy only some elements to another objects, I looked at the web but I don´t found a similar topic

OrioonTV
  • 15
  • 4
  • 1
    welcome to Stackoverflow, we would be happy to help you, please read this once [**Asking Help**](https://stackoverflow.com/help/how-to-ask) so that we know what is the exact problem you face – Aravind Mar 10 '20 at 00:36
  • What have you tried so far? –  Mar 10 '20 at 01:04
  • 1
    also the key words you wish to search on in this case are probably something like "json object delete element" which would lead you to a posting like this which has the elements to solve your problem. https://stackoverflow.com/questions/15451290/remove-element-from-json-object –  Mar 10 '20 at 01:07

2 Answers2

0

Try like any of the below solutions:

const keys = ["titulo" , "categoria","descripcion"];
arrLinks2 = arrLinks.map(item =>{
    let obj = {} ;
    keys.forEach(key =>{
        if(item[key]){
            obj[key] = item[key];
        }
    });
    return obj;
})

or

arrLinks2 =JSON.parse(JSON.stringify(arrLinks)).map(item => {
    delete item.fecha;
    return item;
})
Sayooj V R
  • 2,255
  • 2
  • 11
  • 23
0

Use map method and spread operator ... to get rest properties:

const result = arrLinks.map(({fecha, ...other}) => ({...other}))
console.log(result);

An example:

let arrLinks = [

{
    titulo: "CUADERNO DE BONOS SEGUROS BX+ 2020",
    fecha: "2020-02-18 11:02:41.0",
    categoria: "BONOS E INCENTIVOS",
    descripcion: "Cuaderno de Bonos e Incentivos"
},
{
    titulo: "ACTUALIZACIÓN TARIFA AUTOS / PICKUP 2019",
    fecha: "2019-07-22 10:57:43.0",
    categoria: "CIRCULARES",
    descripcion: "Actualización de tarifa para Seguro de Auto 
        B×+ y Seguro de Pick Up B×+ 2019"
},
  {
    titulo: "AVISO DE ACCIDENTES O ENFERMEDAD - GMM",
    fecha: "2019-09-18 12:42:45.0",
    categoria: "SINIESTROS PERSONAS",
    descripcion: "Aviso de Accidentes o Enfermedad Gastos Médicos Mayores"
  }
];

const result = arrLinks.map(({fecha, ...other}) => ({...other}))
console.log(result);
StepUp
  • 36,391
  • 15
  • 88
  • 148