0

I’ve set up an example in JSFiddle that takes some sample data, manipulates it slightly and then loads this into a new variable. I think this is working.

As an experiment, I’d like to filter this output array where VoucherConfirm equals Y.

Can anyone help with this?

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

// Manipulate Input JSON push result out.

// Create Empty array to store data after manipulation.
let MyOutputArray = {
  Reformated_Stores: []
}
// Get sample data.
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"
    }
  ]
}

// Loop through all sample data.
for (let i = 0; i < inputData.Stores.length; i++) {
  // If the voucher value = 1 set a new variable to Y otherwise set it to N.
  if (inputData.Stores[i].Vouchers === '1') {
    VOUCHER_VAL = "Y";
  }
  else {
    VOUCHER_VAL = "N";
  };
  
  // Reformat the data. Not that Voucher Confirm has been added showing the Y or 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
  }

  // Push newly reformatted data to our originally empty output array.
  MyOutputArray.Reformated_Stores.push(Reformatted_Data);
}

html = "<hr><p id='INPUT_Data_Output'>My INPUT Data =  </p>";
h2.insertAdjacentHTML("beforebegin", html);

// Stringify input data. I do this to allow the resulting string to be diplayed on the page.
const MyinputDataJSON = JSON.stringify(inputData);

// Display string on page.
document.getElementById("INPUT_Data_Output").innerHTML += MyinputDataJSON;

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

// Stringify output data. I do this to allow the resulting string to be diplayed on the page.
const MyOutputArrayJSON = JSON.stringify(MyOutputArray);

// Display string on page.
document.getElementById("Reformatted_Data_Output").innerHTML += MyOutputArrayJSON;

// Script Complete Message.
html = "<p>Script Completed. Please See Console For Any Additional Notes or Errors</p>";
h2.insertAdjacentHTML("afterend", html);
<h2 id="myTitle">Script Output Notes</h2>
<div id="end">---</end>
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Sparx
  • 109
  • 10
  • 2
    `` should be `` – Barmar Dec 19 '22 at 17:37
  • 2
    What's the problem? `array.filter(el => el.VoucherConfirm == 'Y')` – Barmar Dec 19 '22 at 17:38
  • 1
    [Validate your HTML](//validator.nu). See [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/q/284236/4642212). Get familiar with [how to access and process objects or arrays](/q/11922383/4642212), and use the static and instance methods of [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). Looks like you just need `MyOutputArray.Reformated_Stores.filter(({ VoucherConfirm }) => VoucherConfirm === "Y")`. – Sebastian Simon Dec 19 '22 at 17:40

0 Answers0