Skip to content

Instantly share code, notes, and snippets.

@thequbit
Created May 5, 2014 19:18
Show Gist options
  • Save thequbit/bc9aa4f7076b6429ab51 to your computer and use it in GitHub Desktop.
Save thequbit/bc9aa4f7076b6429ab51 to your computer and use it in GitHub Desktop.
<html>
<script>
// return our array of dicts
function getFaultData() {
// create array of dict
var retData = [{
"Name": "Fault A",
"Status": "FAULT",
"FaultBits": [
false,
false,
true,
true,
false,
false
]
}];
// return data
return retData;
}
// populate our global
var faultData = getFaultData();
// function for processing fault data
function processFault(faultIndex) {
// pull the data out of the dict of the array at the faultIndex index
var name = faultData[faultIndex].Name;
var status = faultData[faultIndex].Status;
var faultBits = faultData[faultIndex].FaultBits;
// make a copy of the faultBits array so we can use splice()
faultBitsTemp = faultBits;
// get the first half of the faults using splice for the top
var faultBitsTop = faultBitsTemp.splice(0,faultBitsTemp.length/2);
// since splice is distructive, just set the remainder to the bottom
var faultBitsBottom = faultBitsTemp;
// display our top and bottom new arrays
console.log("faultBitsTop:");
console.log(faultBitsTop);
console.log("faultBitsBottom:");
console.log(faultBitsBottom);
// display our remaining temp array (should be the second part of the original array)
console.log("faultBitsTemp:");
console.log(faultBitsTemp);
// display our original ( should be untouched, and length 6 )
console.log("faultData[faultIndex].FaultBits:");
console.log( faultData[faultIndex].FaultBits );
//
// ^^^ this is incorect, faultBits has been modified!!!
//
}
// execute
processFault( 0 );
</script>
</html>
@thequbit
Copy link
Author

thequbit commented May 5, 2014

Dear Tim, this is the answer. On line 36, replace with the following:

// make a copy of the faultBits array so we can use splice().  splice() is destructive
// so we can't do this on the original.  We can't just use x = y here because javascript.
faultBitsTemp = [].concat(faultBits);

dumb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment