0

i have a doubt that i can not resolve.

I have this data, that i received:

const groupedElements = [
      {
        _id: 'OVTBLNCDYBHP3IKUFQLGQF6AC4',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: '<strong>A 24 horas de que tenga lugar la boda entre el jugador del FC Barcelona Lionel Messi y su pareja Antonella Rocuzzo se ha sabido que un ex jugador del Real Madrid está invitado al enlace.</strong>'
      },
      {
        _id: 'MJQIUVGPPJCSDGYDUFE6SIMB6U',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'Entre los <a href="http://as.com/tikitakas/2017/06/26/portada/1498509190_687200.html" target="_blank">260 invitados a la celebración </a>
          está, cómo no, toda la plantilla del Barça y algunos ex jugadores azulgranas como Ronaldinho, Xavi, Cesc Fábregas o Deco, además de otras grandes figuras del fútbol mundial como Lavezzi y Agüero.'
      },
      {
        _id: '5HL7OES5BJDQPEVVMX3DH4D5AE',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'Desde Diez, sin embargo, informan que estas no serían todas las caras conocidas que acudirán a la mediática boda, 
          que contará con <a href="http://as.com/tikitakas/2017/06/28/portada/1498634766_750691.html" target="_parent">todo tipo de comodidades para los invitados.</a> <strong>Un ex madridista estaría, 
            según la publicación, invitado al enlace y no sería otro que Ángel Di Maria.</strong>'
      },
      {
        _id: 'RBVQCVJDRZGS3OVHO3WW5FITKU',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'El jugador del París Saint-Germain de la Ligue 1 de Francia y el delantero del FC Barcelona son compañeros en la Selección Argentina<strong>, </strong>por lo que esta información parece bastante lógica.'
      },
    ]

I need to access to content property and find if there is some attribute 'target' inside and if this is equal to _parent, remove it.

I tried to to this, first to locate the attribute inside content, but i can not achieve:

const newGroupElements = groupedElements.map(element =>
      element.content.includes("_parent")  ? {...element, content: 'i found you'} : element)

Thanks for your light in my knowledge.

2
  • So you want to squarely delete the content on match? Commented Mar 20, 2022 at 18:47
  • i want to find and delete de target attribute inside content if target=_parent. Commented Mar 20, 2022 at 18:50

2 Answers 2

2

To remove the target attribute in the content string of a matching includes():

const newGroupElements = groupedElements.map(element => {
  if(element.content.includes("_parent")){
    element.content = element.content.replace(/target=".+"/, "")
  }
  return element
})

Or maybe just

const newGroupElements = groupedElements.map(element => element.content.replace(' target="_parent"', ""))
Sign up to request clarification or add additional context in comments.

Comments

-1

This is the function that you need to use :

groupedElements = groupedElements.map(val=>{
       if(val.includes("_parent")&&val.includes("target")) {
          let i = val.indexOf("target");
          val = val.split("");
          val.splice(i,"target='_parent'".length);
          val = val.join("");
       }
       return val;
    })

const groupedElements = [
      {
        _id: 'OVTBLNCDYBHP3IKUFQLGQF6AC4',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: '<strong>A 24 horas de que tenga lugar la boda entre el jugador del FC Barcelona Lionel Messi y su pareja Antonella Rocuzzo se ha sabido que un ex jugador del Real Madrid está invitado al enlace.</strong>'
      },
      {
        _id: 'MJQIUVGPPJCSDGYDUFE6SIMB6U',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'Entre los <a href="http://as.com/tikitakas/2017/06/26/portada/1498509190_687200.html" target="_blank">260 invitados a la celebración </a>\
          está, cómo no, toda la plantilla del Barça y algunos ex jugadores azulgranas como Ronaldinho, Xavi, Cesc Fábregas o Deco, además de otras grandes figuras del fútbol mundial como Lavezzi y Agüero.'
      },
      {
        _id: '5HL7OES5BJDQPEVVMX3DH4D5AE',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'Desde Diez, sin embargo, informan que estas no serían todas las caras conocidas que acudirán a la mediática boda, \
          que contará con <a href="http://as.com/tikitakas/2017/06/28/portada/1498634766_750691.html" target="_parent">todo tipo de comodidades para los invitados.</a> <strong>Un ex madridista estaría, \
            según la publicación, invitado al enlace y no sería otro que Ángel Di Maria.</strong>'
      },
      {
        _id: 'RBVQCVJDRZGS3OVHO3WW5FITKU',
        type: 'text',
        additional_properties: { tag: 'p' },
        alignment: 'center',
        content: 'El jugador del París Saint-Germain de la Ligue 1 de Francia y el delantero del FC Barcelona son compañeros en la Selección Argentina<strong>, </strong>por lo que esta información parece bastante lógica.'
      },
    ]
    groupedElements = groupedElements.map(val=>{
   if(val.includes("_parent")&&val.contains("target")) {
      let i = val.indexOf("target");
      val = val.split("");
      val.splice(i,"target='_parent'".length);
      val = val.join("");
   }
   return val;
})
    console.log(groupedElements)
    

2 Comments

target is inside a string in the content property. There is no target property to delete...
Ohh ... understood .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.