如何重命名数组中的对象?

发布于 2025-01-17 08:11:29 字数 616 浏览 0 评论 0原文

我正在尝试使用此 API,但是我想通过 strMeasure & 使用 for 循环或映射。字符串成分。目前,这些附件的数量少于 20 个。所以我想我需要先使用 for 循环创建一个数组,然后再实现它?让我知道你的想法..

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;
});

但随后不知道如何将其注入我的组件中。

干杯!

API

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2025-01-24 08:11:29

我建议不要将它们连接到字符串,而是将它们存储为对象中的键值对。这样可以更轻松地访问 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",
//   },
//   …
// ];

转换数据后,您可以轻松地将其传递给您的组件

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} />;

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} />;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文