Delete Specific Array within another Array

-1

I have a global array that stores more arrays inside. The internal arrays store two values, the x and y of the position of a graph.

Well then, with that said, I explain how it works for now and what I need.

I have (for now, in the future it will be more) two checkboxes, when they are activated I save the data that is mentioned in the checkbox inside the parent array. This data is an Array with two values (as I said before). Adding them is not a problem, use push and that's it, the problem is to eliminate them when a checkbox is deselected.

I have this code:

//Checkbox para mostrar data eaepunt1
                $("#eae").change(function()
                {
                    mostrarDatos();
                });

                //Checkbox para mostrar data arabpunt1
                $("#arab").change(function()
                {
                    mostrarDatos();
                });


                function mostrarDatos(){
                    if($(eae).prop('checked') == true)
                    {   
                        totaldata.push(eaepunt1[coundata])                      
                    }
                    else
                    {
                        //Borrar los datos del checkbox
                    }

                    if($(arab).prop('checked') == true)
                    {
                        totaldata.push(arabpunt1[coundata])                     
                    }
                    else
                    {   
                        //Borrar datos del checkbox
                    }
                    gr_html.series[0].data = totaldata;         
                    gr_html.redraw();                   

                }

At first you could empty the parent Array and re-fill it every time I run the function, but it seems like a bit of a "sloppy" solution. How should I do it?

    
asked by Lombarda Arda 15.03.2017 в 12:31
source

2 answers

1

I see no problem in making a copy of your original array just to redraw your graphic, adding the optional data (determined by the check ) elements to that copy.

That is:

function mostrarDatos() {
    var salida = totaldata.slice();     // copia del arreglo original

    if ($(eae).prop('checked') == true) { 
        salida.push(eaepunt1[coundata]);
    }

    if ($(arab).prop('checked') == true) {
        salida.push(arabpunt1[coundata]);
    }

    gr_html.series[0].data = salida;
    gr_html.redraw();
}

That way you do not have to be "tracking" which elements belong to which check to be able to remove them from your original array (something that would be complicated by adding new optional elements) .

If you need said array complete for something else, then you can generate a new helper function to help you with that; something in these lines:

function generarDatos() {
    var salida = totaldata.slice();

    if ($(eae).prop('checked') == true) { 
        salida.push(eaepunt1[coundata]);
    }

    if ($(arab).prop('checked') == true) {
        salida.push(arabpunt1[coundata]);
    }

    return salida;        
}

function mostrarDatos() {
    gr_html.series[0].data = generarDatos();
    gr_html.redraw();
}

Copying an array object should not cause a bottleneck (unless it contains a very large number of elements); If you want to try different methods to make copies, you can review this benchmark .

    
answered by 15.03.2017 в 18:37
0

I do not know if I understood the question very well, but I think that with slice e indexOf you could solve it.

function mostrarDatos(){
                    if($(this).prop('checked') == true)
                    {   
                        totaldata.push(eaepunt1[coundata])                      
                    }
                    else
                    {
                        totaldata.slice(totaldata.indexOf(eaepunt1[coundata]),1)
                    }

                    if($(this).prop('checked') == true)
                    {
                        totaldata.push(arabpunt1[coundata])                     
                    }
                    else
                    {   
                        totaldata.slice(totaldata.indexOf(arabpunt1[coundata]),1)
                    }
                    gr_html.series[0].data = totaldata;         
                    gr_html.redraw();                   

                }

Greetings!

    
answered by 15.03.2017 в 13:04