2

I have an array of arrays in JavaScript that I'm storing some values in, and I'm attempting to find a way to clear the value within that array when the user removes the specified control from the page, however I'm not finding a good way to do this and anything I try doesn't seem to be working.

What is the best method for clearing the value in the array? I'd prefer the value to be null so that it's skipped when I iterate over the array later on.

I've tried to do MyArray[id][subid] = '' but that still is technically a value. I've also tried to do MyArray[id][subid].length = 0 but that doesn't seem to do anything either. Trying to grab the index and splice it from the array returns a -1 and therefore doesn't work either.

var MyArray;

window.onload = function(){
     MyArray = new Array();
}

function EditValuesAdd(){

     var Input = document.getElementById('Values-Input').value;
     var ID = document.getElementById('FID').value;
     var ValueID = ControlID(); // generate GUID
     if (!MyArray[ID]) MyArray[ID] = new Array();
     MyArray[ID][ValueID] = Input;
     document.getElementById('Values').innerHTML += '<a href="#" id="FV-' + ValueID + '" onclick="EditValuesRemove(this.id)"/><br id="V-' + ValueID + '"/>';
}

function EditValuesRemove(id)
{
     var ID = document.getElementById('FID').value;
     document.getElementById(id).remove();
     document.getElementById(id.replace('FV-', 'V-')).remove();
     MyArray[ID][id.replace('FV-', '')] = '';
}

I've also tried to do an index of and then splice it from the underlying array but the index always returns -1.

var Index = MyArray[ID].indexOf(id.replace('FV-', ''));
MyArray[ID].splice(Index, 1);

Setting the length to zero has no effect either.

MyArray[ID][id.replace('FV-', '')].length = 0;

I would expect that one of the methods above would clear out the value and make it null so that it is skipped later on but all of the methods I've found and tried so far leave some non-null value.

11
  • 2
    If ControlID() returns a GUID string, you're misusing arrays. Commented May 15, 2019 at 12:26
  • Try delete MyArray[id][subid]; Sorry wrong language at my first try ;-) Commented May 15, 2019 at 12:26
  • Are you able to give an example of what the array looks like and how you want it to look after the method has been executed? Commented May 15, 2019 at 12:26
  • 1
    @Adder where does unset() come from? Commented May 15, 2019 at 12:27
  • 4
    I think an Object {} instead of an Array [] would suit better your use case. Once you're using objects, check delete Commented May 15, 2019 at 12:28

2 Answers 2

1

What you need is an object (a Map), not an array (a list).

Here's a basic idea of how to do it :

MyArray = {};
....
if (!MyArray[ID]) MyArray[ID] = {}
MyArray[ID][ValueID] = Input;
...
delete MyArray[ID][id.replace('FV-', '')];

Check here for more information : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Sign up to request clarification or add additional context in comments.

Comments

0

In the end I used an array of objects MyArray = [] and then using splice/findindex to remove it from the array:

function RemoveItem(id)
{
   var Index = MyArray.findIndex(a => a.ID == id.replace('FV-', ''));
   MyArray.splice(Index, 1);
   document.getElementById(id).remove();
   document.getElementById('FVB-' + id.replace('FV-', '')).remove();
}

It doesn't solve the actual question asked but I don't know if there really is an answer since I was using arrays in the wrong manner. Hopefully this at least points someone else in the right direction when dealing with arrays and objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.