I have the code below that takes the subKeyValuesList and removes values from it as the loop iterates. This works however after each loop (it's one big array containing 4 arrays) I want the subKeyValuesList values to reset to the original value. Below I've tried to reset by applying some code to the last iteration of the loops but no joy. I think it may be a scope issue but have struggled to see where the issue is
var subKeyValuesList = ['0009397b-563f-4dfc-8022-1516dbdffbda','1358d485-69e6-43bb-bc2f-ff25baa6a810']
for (var fi = 0; fi < fields.length; fi++) {
var subKeyValuesList2 = subKeyValuesList
var fieldSplitTemp = fieldLong.replace(/[`~!@#$%^&*()_|+\=?;:'"<>\{\}\[\]\\\/]/gi, 'x')
var fieldSplit = fieldSplitTemp.split("xix.")
var fieldCount = fieldSplit.length
var arrayPath = fieldSplit.splice(0, fieldCount - 1)
for (let i = 0; i < eval(arrayPath[0]).length; i++) {
for (let ii = 0; ii < eval(arrayPath[0] + "[i]." + arrayPath[1]).length; ii++) {
var keyNames = Object.keys(eval(arrayPath[0] + "[i]." + arrayPath[1] + "[ii]"));
var subKeyName = keyNames[0]
var parent = eval(arrayPath[0] + "[i]." + arrayPath[1] + "[ii]." + subKeyName)
var index = subKeyValuesList2.indexOf(parent);
if (index !== -1) {
subKeyValuesList2.splice(index, 1)
}
if (ii == eval(arrayPath[0] + "[i]." + arrayPath[1]).length - 1) {
//if last instance in loop do reset spliced list with original subKeyValuesList
subKeyValuesList2 = subKeyValuesList
}
}
}
}