首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数组中的对象显示在文档或控制台上?

如何将数组中的对象显示在文档或控制台上?
EN

Stack Overflow用户
提问于 2018-10-08 20:52:59
回答 5查看 41关注 0票数 1

我试图制作一个基本的库存保存工具,在这个工具中,用户可以输入条形码并接收与该条形码相关的产品信息,如产品名称和数量等。

为了组织我的数据,我创建了一个名为products的数组,并为每个产品创建了一个对象。

我的数组当前如下所示:

代码语言:javascript
复制
 var products = [
 {
  brand:"Healthy Boy",
  product:"Sweet Chilli Sauce",
  size: 300,
  measurement: "ml",
  barcode:"00909274636143",
  quantity:"2"
 },
 {
  brand:"Golden Dragon",
  product:"rice",
  size: 1,
  measurement: "kg",
  barcode:"5623593845",
  quantity:"5"
 },
 {
  brand:"Golden Dragon",
  product:"rice",
  size: 1,
  measurement: "kg",
  barcode:"5623593845",
  quantity:"5"
 }
];

我使用两个函数通过输入条形码来搜索,以查看条形码是否是数组的一部分。函数如下所示:

代码语言:javascript
复制
function isBarcodeValid (barcode){
for(var i = 0; i < products.length; i++) {
    if(products [i].barcode === barcode){
        return true;
    }
}
return false;
}

function displayBarcode (barcode){
if (isBarcodeValid(barcode)){
    console.log("barcode exists");
} else {
    console.log("invalid barcode");
}
}

目前,我正在登录到控制台,无论条形码是否存在于数组中,但是是否有一种方法可以显示与对象中包含的每个条形码相关的信息?

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-10-08 21:05:47

而不是返回布尔值,而是返回对象或null:

代码语言:javascript
复制
function isBarcodeValid (barcode){
  for(var i = 0; i < products.length; i++) {
     if(products [i].barcode === barcode){
      return products[i];
     }
  }
  return null;
}

function displayBarcode (barcode){
  var v = isBarcodeValid(barcode);
  if (v){
    console.log(v);
  } else {
    console.log("invalid barcode");
  }
}
票数 4
EN

Stack Overflow用户

发布于 2018-10-08 21:05:24

代码语言:javascript
复制
 var products = [
 {
  brand:"Healthy Boy",
  product:"Sweet Chilli Sauce",
  size: 300,
  measurement: "ml",
  barcode:"00909274636143",
  quantity:"2"
 },
 {
  brand:"Golden Dragon",
  product:"rice",
  size: 1,
  measurement: "kg",
  barcode:"5623593845",
  quantity:"5"
 },
 {
  brand:"Golden Dragon",
  product:"rice",
  size: 1,
  measurement: "kg",
  barcode:"5623593845",
  quantity:"5"
 }
];

function findBarcode (barcode){
  for(var i = 0; i < products.length; i++) {
    const p = products[i]
      if(p.barcode === barcode){
          return p;
      }
  }
}

function displayBarcode (barcode){
  const barCode = findBarcode(barcode)
  const barcodeIsValid = !!barCode
  if (barcodeIsValid){
      console.log(barCode, "barcode exists");
  } else {
      console.log("invalid barcode");
  }
}

displayBarcode('00909274636143') //present
displayBarcode('not-a-barcode') //present

您可以在筛选时返回标识的产品以查看它是否存在。

票数 1
EN

Stack Overflow用户

发布于 2018-10-08 21:23:42

您可以利用javascript中的Array方法和ES6语法返回产品,以执行查找,这是对for的解决办法,并在中间中断for循环调整数据:

代码语言:javascript
复制
'use strict';

const products = [{
  brand: 'Healthy Boy',
  product: 'Sweet Chilli Sauce',
  size: 300,
  measurement: 'ml',
  barcode: '00909274636143',
  quantity: '2',
  }, {
    brand: 'Golden Dragon',
    product: 'rice',
    size: 1,
    measurement: 'kg',
    barcode: '5623593845',
    quantity: '5',
 }, {
  brand: 'Golden Dragon',
  product: 'rice',
  size: 1,
  measurement: 'kg',
  barcode: '5623593845',
  quantity: '5',
}];


/**
 * Look for a given barcode and return the product or null if barcode doesn't exist
 * @param {String} barcode
 * @return {Object} product data or null
 * */
function isBarcodeValid (barcode){
  return products.find(p => p.barcode === barcode);
}

function displayBarcode (barcode) {
  const product = isBarcodeValid(barcode);
  if (product){
    console.log(`barcode exists: ${product}`);
  } else {
      console.log('invalid barcode');
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52709950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档