> Faqs > ¿Cómo guardar contenido HTML como valor dentro de una sola propiedad en un objeto de javascript?

¿Cómo guardar contenido HTML como valor dentro de una sola propiedad en un objeto de javascript?

Hola,

Quisiera poder guardar todo el contenido html en un objecto de js y quisiera que todo quede almacenado dentro de una misma propiedad. ¿Cuál es la manera más efectiva de hacer esto?

export const servicesdata = [
  {
    id: 1,
    title: "Physiotherapy",
    image: "https://atriumphysiotherapy.com/images/stretching.jpg",
    descriptionp1:
      "Our physiotherapists are highly-educated experts in physical function, movement and mobility. They have advanced knowledge of how the human body moves and what stops it moving and use specialized hands-on treatment to restore, maintain and maximize optimal function and quality of life.",
    descriptionp2:
      "Our physiotherapists assists, diagnose and treat physical symptoms and limited movement caused by injury, aging, disability, or medical condition. They help patients understand what’s causing their condition, and work with them to restore, maintain and maximize movement, flexibility and physical independence.",
    descriptionp3:
      "Our physiotherapists develop customized treatment plans that help patients take back control. They teach patients how to restore, maintain and/or maximize movement, reduce pain, and manage any chronic symptoms.",
    descriptionp4:
      "Our physiotherapists excel in the treatment of many conditions: back pain/injury, whiplash, pregnancy-related muscle/joint issues, urinary incontinence and vertigo to name a few. They also help patients manage symptoms of chronic conditions like arthritis and chronic pain.",
    descriptionp5:
      "Our physiotherapists help patients who may have otherwise tried temporary (e.g., prescription drugs) or more invasive methods (e.g., surgery) to manage their condition. They use individualized therapeutic exercise, manual therapy and other treatment techniques.",
    descriptionp6:
      "Through physiotherapy, many patients are able to recover unrestricted movement which promotes the kind of physical independence necessary for a normal lifestyle and work. When it comes to assessing and treating people with movement problems, the services of a physiotherapist are often essential.",
    image: "https://atriumphysiotherapy.com/images/stretching.jpg",
    subtitle: "What Can I Expect During My First Appointment?",
  },
  {
    id: 2,
    title: "123",
    image: "../../assets/images/servicesimg/physiotherapy.jpg",
    subtitle:
      "Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe quas iusto exercitationem possimus nam corrupti? Debitis hic quo accusamus aliquam? Nobis deleniti ducimus impedit ad aut, numquam ipsa nam eveniet.",
  }]

Respuestas

No se entiende bien lo que necesitas hacer.

Si tienes que guardar contenido en un objeto de Javascript puedes guardarlo en una propiedad cualquiera que te venga bien. Como En Javascript puedes inventarte el nombre de cualquier propiedad da igual el nombre que le pongas.

// tienes un objeto
let obj = {}

// Ahora guardas contenido en él
obj.miPropiedadParaGuardarContenido = `
    <p>Este es el contenido que vamos a guardar</p>
    <p>Como puedes ver, es contenido <b>HTML</b></p>
`;

Si fuera el contenido de una etiqueta en particular:

let etiqueta = document.getElementById('una-etiqueta');
obj.miPropiedadParaGuardarContenido = etiqueta.innerHTML;
Lucas
139 1 8 5
Lo que estoy intententando hacer es agregar html dinamico que tiene que cambiar cuando el usuario hace click en diferentes elementos de un grid. Este contenido esta guardado en una matriz de objectos. El problema que tengo es que cuando se renderiza el contenido, todo el texto figura en forma junta, osea sin espacios