打印库存数据

发布于 2025-01-13 02:36:01 字数 424 浏览 1 评论 0原文

我想打印库存数据:

const inventory = require('./cars.js');

console.log(inventory[id])

数据是这样的:

let inventory = [
  { id: 1, car_make: 'Lincoln', car_model: 'Navigator', car_year: 2009 },
  { id: 2, car_make: 'Mazda', car_model: 'Miata MX-5', car_year: 2001 },
  {
    id: 3,
    car_make: 'Land Rover',
    car_model: 'Defender Ice Edition',
    car_year: 2010,
  },
];

I want to print inventory data:

const inventory = require('./cars.js');

console.log(inventory[id])

the data is like this:

let inventory = [
  { id: 1, car_make: 'Lincoln', car_model: 'Navigator', car_year: 2009 },
  { id: 2, car_make: 'Mazda', car_model: 'Miata MX-5', car_year: 2001 },
  {
    id: 3,
    car_make: 'Land Rover',
    car_model: 'Defender Ice Edition',
    car_year: 2010,
  },
];

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

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

发布评论

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

评论(1

离笑几人歌 2025-01-20 02:36:01

您可以循环查看库存列表并按 ID 查找商品。

const inventory = [
  {
    id: 1,
    car_make: "Lincoln",
    car_model: "Navigator",
    car_year: 2009,
  },
  {
    id: 2,
    car_make: "Mazda",
    car_model: "Miata MX-5",
    car_year: 2001,
  },
  {
    id: 3,
    car_make: "Land Rover",
    car_model: "Defender Ice Edition",
    car_year: 2010,
  },
];

function getInventoryById(inventory, inventoryId) {
  return inventory.find(item => item.id === inventoryId);
}

const item = getInventoryById(inventory, 3)

// item:
// {
//   id: 3,
//   car_make: 'Land Rover',
//   car_model: 'Defender Ice Edition',
//   car_year: 2010
// }

You can loop over your inventory list and look up the item by ID.

const inventory = [
  {
    id: 1,
    car_make: "Lincoln",
    car_model: "Navigator",
    car_year: 2009,
  },
  {
    id: 2,
    car_make: "Mazda",
    car_model: "Miata MX-5",
    car_year: 2001,
  },
  {
    id: 3,
    car_make: "Land Rover",
    car_model: "Defender Ice Edition",
    car_year: 2010,
  },
];

function getInventoryById(inventory, inventoryId) {
  return inventory.find(item => item.id === inventoryId);
}

const item = getInventoryById(inventory, 3)

// item:
// {
//   id: 3,
//   car_make: 'Land Rover',
//   car_model: 'Defender Ice Edition',
//   car_year: 2010
// }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文