How Exlude, remove variable in array ?

Hi guys
I have a complex problem in a formula.
I formulate to minimum to be clear.
I wish I could remove the first variable found in the second process.
These possible?


// variable initialisation
A = 3
B = 5
T = 1
X = 4
// Now found the most big valur
ItemQ = new Array(A,B,T,X); 
ItemL = 0; 
for(n = 0; n < ItemQ.length; n++) 
{ if (ItemQ[n] > ItemQ[ItemL]) 
    ItemL = n;
} 
// EXAMPLE HERE ITS FOUND (😎 MOST HIGTH VALUR
// Now a need to do again, but exclud B  _.without()
without = ItemL; // maybe store B in witout ??? or ...
ItemQ = new Array.without()(A,B,T,X); 
ItemL = 0; 
for(n = 0; n < ItemQ.length; n++) 
{ if (ItemQ[n] > ItemQ[ItemL]) 
    ItemL = n;
} 


Tank you


Here is the full version, but this solution does not please me.

Spoiler
Code:
 // variable initialisation
D4 = $gameVariables.value(18), D6 = $gameVariables.value(19), D8 = $gameVariables.value(20), D12 = $gameVariables.value(21), D20 = $gameVariables.value(22), 
D30 = $gameVariables.value(23), D50 = $gameVariables.value(24), D100 = $gameVariables.value(25);
// take quantity of the 8 item , ref by variable
ItemQ = new Array(D4,D6,D8,D12,D20,D30,D50,D100); 
ItemL = 0; 
for(n = 0; n < ItemQ.length; n++) 
{ if (ItemQ[n] > ItemQ[ItemL]) 
	ItemL = n;
} 
console.log (ItemL, ItemQ[ItemL]);
$gameVariables.setValue(32, ItemL);
 // On recommence mais sans la derniere valeur
switch (ItemL)
{case 0: D4 = 0; break;case 1: D6 = 0; break;case 2: D8 = 0; break;case 3: D12 = 0; break;case 4: D20 = 0; break;case 5: D30 = 0; break;case 6: D50 = 0; break;case 7: D100 = 0; break;}
 ItemQ = new Array(D4,D6,D8,D12,D20,D30,D50,D100); 
 ItemL = 0; 
 for(n = 0; n < ItemQ.length; n++) { if (ItemQ[n] > ItemQ[ItemL])ItemL = n;}
 console.log (ItemL, ItemQ[ItemL]);
 $gameVariables.setValue(33, ItemL);
I've moved this thread to Javascript Support. Please be sure to post your threads in the correct forum next time. Thank you.
Well, I don't completely understand what you want to do so I'm going to rephrase it so you can confirm.


You want to :


-find the highest value in an array


-remove it


If that's what you want and are OK with modifying the base array, you could return the index of the maximum value and use splice to remove it.


ar.splice(index,length)
//index is where you want to start removing, maximum value index here
//length is how many elements you want to remove from there, 1 in this case.


You'll have to decide and take into account what happens if you have two values that happen to be the same and the maximum, remove the first found or the last found?


Also: it's bad practive to declare arrays like this, just do this:


var ar = [a,b,c,d]


Check this website out for examples and an introduction to arrays, I learned a lot from it:


http://www.w3schools.com/js/js_array_methods.asp


It also provides examples as to why it's bad practice to declare arrays like this.
My best bet here is this if i understood correctly


// SAFE VERSION (keeps the array you give in intact)
// "out_values" is how many biggest number you want as an output
sort_biggest_values = function(values, out_values) {
if (out_values <= 0 || values.length == 0)
return [];
else if (values.length == 1) {
// gives a new arary instead of the old one to keep him intact
// if you dont' need it use "return values;"
return new Array(values[0]);
}

// if you dont' need to keep original array intact you can change
// this line in "var work_array = values;"
var work_array = clone_array(values);
var output_array = [];

for (var i = 0; i < values.length; i++) {

}

for (var i = 0; i < out_values || i < values.length; i++) {
var value_to_remove_id;
var biggest_value = values[0];
for (var j = 1; j < values.length; j++) {
if (values[j] > biggest_value) {
biggest_value = values[j];
value_to_remove_id = j;
}
}

work_array.splice(value_to_remove_id, 1);
output_array = biggest_value;
}

return output_array;
};

clone_array = function(source) {
var target = [];
for (var i = 0; i < source.length; i++) {
target = source;
}
return target;
};




This is how u use the script


// EXAMPLE ON HOW TO USE THIS METHOD
var A = 10;
var B = 30;
var C = 20;
var D = 40;

var values_to_sort = new Array(A,B,C,D);
var sorted values = sort_biggest_values(values_to_sort, 3); // result is [40,30,20]


i didnt' test it but it should work, let me know 🙂


EDIT


Apparently i'm a noob... you can basically do all this with


var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a}); // Sort the numbers in the array in descending order
// The first item in the array (points[0]) is now the highest value


After that if you want to redure the array length you can just type like points.length = 2;


If you want to ahve all this in a funciton it will be like:

Code:
// SAFE VERSION (keeps the array you give in intact)
// "out_values" is how many biggest number you want as an output
sort_biggest_values = function(values, out_values) {
    if (out_values <= 0 || values.length == 0)
        return [];
    else if (values.length == 1) {
        // gives a new arary instead of the old one to keep him intact
        // if you dont' need it use "return values;"
        return new Array(values[0]);
    }
    
    // if you dont' need to keep original array intact you can change
    // this line in "var work_array = values;"
    var work_array = clone_array(values);
    work_array.sort(function(a, b){return b-a});

    return work_array;
};

clone_array = function(source) {
    var target = [];
    for (var i = 0; i < source.length; i++) {
        target[i] = source[i];
    }
    return target;
};
My best bet here is this if i understood correctly



// SAFE VERSION (keeps the array you give in intact)
// "out_values" is how many biggest number you want as an output
sort_biggest_values = function(values, out_values) {
if (out_values <= 0 || values.length == 0)
return [];
else if (values.length == 1) {
// gives a new arary instead of the old one to keep him intact
// if you dont' need it use "return values;"
return new Array(values[0]);
}

// if you dont' need to keep original array intact you can change
// this line in "var work_array = values;"
var work_array = clone_array(values);
var output_array = [];

for (var i = 0; i < values.length; i++) {

}

for (var i = 0; i < out_values || i < values.length; i++) {
var value_to_remove_id;
var biggest_value = values[0];
for (var j = 1; j < values.length; j++) {
if (values[j] > biggest_value) {
biggest_value = values[j];
value_to_remove_id = j;
}
}

work_array.splice(value_to_remove_id, 1);
output_array = biggest_value;
}

return output_array;
};

clone_array = function(source) {
var target = [];
for (var i = 0; i < source.length; i++) {
target = source;
}
return target;
};




This is how u use the script



// EXAMPLE ON HOW TO USE THIS METHOD
var A = 10;
var B = 30;
var C = 20;
var D = 40;

var values_to_sort = new Array(A,B,C,D);
var sorted values = sort_biggest_values(values_to_sort, 3); // result is [40,30,20]


i didnt' test it but it should work, let me know 🙂


EDIT


Apparently i'm a noob... you can basically do all this with



var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a}); // Sort the numbers in the array in descending order
// The first item in the array (points[0]) is now the highest value


After that if you want to redure the array length you can just type like points.length = 2;


If you want to ahve all this in a funciton it will be like:



// SAFE VERSION (keeps the array you give in intact)
// "out_values" is how many biggest number you want as an output
sort_biggest_values = function(values, out_values) {
if (out_values <= 0 || values.length == 0)
return [];
else if (values.length == 1) {
// gives a new arary instead of the old one to keep him intact
// if you dont' need it use "return values;"
return new Array(values[0]);
}

// if you dont' need to keep original array intact you can change
// this line in "var work_array = values;"
var work_array = clone_array(values);
work_array.sort(function(a, b){return b-a});

return work_array;
};

clone_array = function(source) {
var target = [];
for (var i = 0; i < source.length; i++) {
target = source;
}
return target;
};




tank you for this super info, thîs will help me
Consult this link if you want to do things with arrays, they are extensively used so there are a lot of useful methods already implemented for them


ARRAY_METHODS_AND_PROPERTIES

Original thread: https://forums.rpgmakerweb.com/threads/how-exlude-remove-variable-in-array.64607/