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

3
  • 1
    welcome to Stackoverflow, we would be happy to help you, please read this once Asking Help so that we know what is the exact problem you face Commented Mar 10, 2020 at 0:36
  • What have you tried so far? Commented Mar 10, 2020 at 1: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. stackoverflow.com/questions/15451290/… Commented Mar 10, 2020 at 1:07

2 Answers 2

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;
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @SAYOOJ V R that was exactly i was looking
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.