Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How to populate a HTML div from array object?

I was YouTube video on ReactJS where it populate a div container from a array object using .map() . Is there any way to do it using vanilla js or jquery in simple HTML ? ReactJS code is provided below -

ProductCategories.JSX

import { categories } from "../dataList";
import CategoryItem from "./CategoryItem";

<div className="* * *">
  {categories.map((item) => (
  <CategoryItem item={item} key={item.id} />
  ))}
</div>

CategoryItem.JSX

import styled from "styled-components";

<Container>
  <Image src={item.Image} />
  <Info>
    <Title>{item.Title}</Title>
    <Button>SHOP NOW</Button>
  </Info>
</Container>

dataList.js

export const categories = [
  {
    id: 1,
    Image: "https://i.ibb.co/CnkbTqm/download-ixid-Mnwx.jpg",
    Title: "Lorem",
  },
  {
    id: 2,
    Image: "https://i.ibb.co/CnkbTqm/download-ixid.jpg",
    Title: "Ipsum",
  },
  {
    id: 3,
    Image: "https://i.ibb.co/CnkbTqm/download.png",
    Title: "lorem",
  },
];
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

One way, using reduce():

  1. Loop through the categories using reduce(), for each object return a string (concatenating the previous value) that contains:
    1. A <h1> created with cat.Title
    2. A <img> created with cat.Image
  2. Set the string as innerHTML of your element
    Using <body> as demo

const categories = [{id: 1, Image: "https://i.ibb.co/CnkbTqm/download-ixid-Mnwx.jpg", Title: "Lorem", }, {id: 2, Image: "https://i.ibb.co/CnkbTqm/download-ixid.jpg", Title: "Ipsum", }, {id: 3, Image: "https://i.ibb.co/CnkbTqm/download.png", Title: "lorem", }, ];

document.body.innerHTML = categories.reduce((prev, cat) => prev + `<h2>${cat.Title}</h2> <img src='${cat.Image}' />`, '');


We can get the same idea/behaviour/output using map() and join() instead off reduce() like so:

const categories = [{id: 1, Image: "https://i.ibb.co/CnkbTqm/download-ixid-Mnwx.jpg", Title: "Lorem", }, {id: 2, Image: "https://i.ibb.co/CnkbTqm/download-ixid.jpg", Title: "Ipsum", }, {id: 3, Image: "https://i.ibb.co/CnkbTqm/download.png", Title: "lorem", }, ];

document.body.innerHTML = categories.map(cat => `<h2>${cat.Title}</h2> <img src='${cat.Image}' />`).join('');

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use createElement() api https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

here are some ideas in vanilla

var div = document.createElement('div');
var link = document.createElement('a');
var article = document.createElement('article');

and here in Jquery

var elem = $('<div></div>')

you can create element and append to the them container.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda