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

147
Vistas
How to rename objects in array?

I am trying to use this API however, I want to use a for loop or map through the strMeasure & strIngredient. Currently those have numbers less than 20 attached. So I am thinking I need to make an array first using a for loop and then implement it? Let me know your thoughts..

const [recipeData, setRecipeData] = useState([]);
const ingredientList = recipeData.map((allData) => {
  const arr = [];
  for (let i = 1; i <= 20; i++) {
    arr.push(allData[`strMeasure${i}`] + " " + allData[`strIngredient${i}`]);
  }
  return arr;
});

But then do not know how to inject this in my component.

Cheers!

API

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Instead of jointing them to a string, I would suggest storing them as key, value pairs within an object. This way it is easier to access the values in your UI.

const allData = {
  strIngredient1: "Beef",
  strIngredient2: "Plain Flour",
  strMeasure1: "1kg",
  strMeasure2: "2 tbs",
  …
};

const items = [];

for (let i = 1; i <= 20; i += 1) {
  const strIngredient = allData[`strIngredient${i}`];
  const strMeasure = allData[`strMeasure${i}`];

  items.push({
    ingredient: strIngredient,
    measure: strMeasure,
  });
}

// const items = [
//   {
//     ingredient: "Beef",
//     measure: "1kg",
//   },
//   …
// ];

After you transformed your data, you can pass it easily to your component

function Recipe(props) {
  const { items } = props.items;

  return (
    <ul>
      {items.map((item) => (
        <li key={item.ingredient}>
          {item.ingredient}: {item.measure}
        </li>
      ))}
    </ul>
  );
}

<Recipe items={items} />;
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