You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何筛选输出数组中VoucherConfirm等于Y的元素

解决方案:筛选VoucherConfirm为Y的元素

你可以通过两种方式实现需求:

方法一:生成数据时直接过滤(更高效)

在生成格式化数据后,只将VoucherConfirm为Y的元素推入结果数组,避免生成不必要的数据:

const h2 = document.getElementById("end");

// 初始化结果数组
let MyOutputArray = {
  Reformated_Stores: []
}

const inputData = {
  Stores: [
    {
      'AccountNumber': '10',
      'Store_ID': '1073741825',
      'Store_NAME': 'Store A',
      'Vouchers': "0"
    },
    {
      'AccountNumber': '20',
      "Store_ID": "1073741826",
      "Store_NAME": "Store B",
      "Vouchers": "0"
    },
    {
      "AccountNumber": "30",
      "Store_ID": "1073741827",
      "Store_NAME": "Store C",
      "Vouchers": "1"
    }
  ]
}

// 遍历处理数据
for (let i = 0; i < inputData.Stores.length; i++) {
  const VOUCHER_VAL = inputData.Stores[i].Vouchers === '1' ? "Y" : "N";
  
  const Reformatted_Data = {
    "Status": "Active",
    "Account": +inputData.Stores[i].AccountNumber,
    "ProdID": +inputData.Stores[i].Store_ID,
    "VouchersAvailable": +inputData.Stores[i].Vouchers,
    "VoucherConfirm": VOUCHER_VAL
  }

  // 只推入VoucherConfirm为Y的元素
  if (VOUCHER_VAL === "Y") {
    MyOutputArray.Reformated_Stores.push(Reformatted_Data);
  }
}

// 页面渲染部分保持不变
html = "<hr><p id='INPUT_Data_Output'>My INPUT Data =  </p>";
h2.insertAdjacentHTML("beforebegin", html);

const MyinputDataJSON = JSON.stringify(inputData);
document.getElementById("INPUT_Data_Output").innerHTML += MyinputDataJSON;

html = "<p id='Reformatted_Data_Output'>My Reformatted Data =  </p>";
h2.insertAdjacentHTML("beforebegin", html);

const MyOutputArrayJSON = JSON.stringify(MyOutputArray);
document.getElementById("Reformatted_Data_Output").innerHTML += MyOutputArrayJSON;

html = "<p>Script Completed. Please See Console For Any Additional Notes or Errors</p>";
h2.insertAdjacentHTML("afterend", html);

方法二:生成完整数组后再筛选(更灵活)

如果需要保留完整的格式化数组,之后再通过filter方法筛选出符合条件的元素:

const h2 = document.getElementById("end");

// 初始化结果数组
let MyOutputArray = {
  Reformated_Stores: []
}

const inputData = {
  Stores: [
    {
      'AccountNumber': '10',
      'Store_ID': '1073741825',
      'Store_NAME': 'Store A',
      'Vouchers': "0"
    },
    {
      'AccountNumber': '20',
      "Store_ID": "1073741826",
      "Store_NAME": "Store B",
      "Vouchers": "0"
    },
    {
      "AccountNumber": "30",
      "Store_ID": "1073741827",
      "Store_NAME": "Store C",
      "Vouchers": "1"
    }
  ]
}

// 遍历处理数据(原逻辑不变)
for (let i = 0; i < inputData.Stores.length; i++) {
  const VOUCHER_VAL = inputData.Stores[i].Vouchers === '1' ? "Y" : "N";
  
  const Reformatted_Data = {
    "Status": "Active",
    "Account": +inputData.Stores[i].AccountNumber,
    "ProdID": +inputData.Stores[i].Store_ID,
    "VouchersAvailable": +inputData.Stores[i].Vouchers,
    "VoucherConfirm": VOUCHER_VAL
  }

  MyOutputArray.Reformated_Stores.push(Reformatted_Data);
}

// 新增筛选逻辑:保留VoucherConfirm为Y的元素
MyOutputArray.Reformated_Stores = MyOutputArray.Reformated_Stores.filter(item => item.VoucherConfirm === "Y");

// 页面渲染部分保持不变
html = "<hr><p id='INPUT_Data_Output'>My INPUT Data =  </p>";
h2.insertAdjacentHTML("beforebegin", html);

const MyinputDataJSON = JSON.stringify(inputData);
document.getElementById("INPUT_Data_Output").innerHTML += MyinputDataJSON;

html = "<p id='Reformatted_Data_Output'>My Reformatted Data =  </p>";
h2.insertAdjacentHTML("beforebegin", html);

const MyOutputArrayJSON = JSON.stringify(MyOutputArray);
document.getElementById("Reformatted_Data_Output").innerHTML += MyOutputArrayJSON;

html = "<p>Script Completed. Please See Console For Any Additional Notes or Errors</p>";
h2.insertAdjacentHTML("afterend", html);

说明

  • 方法一在生成数据时直接过滤,减少数组操作次数,性能更优;
  • 方法二适合需要先保留完整数据集,后续再根据条件筛选的场景,灵活性更高。

内容的提问来源于stack exchange,提问作者Sparx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 14:50:28