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>