Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!