如果數組中的第一個值高於或低於其他值,推薦使用哪種方法進行比較。將數組中的第一個值與其他值進行比較
我有一個數組如下面
VAR一個= [8,3,114,34,0,2]
我想比較,如果值A [0]是高於或低於其他數組值通過js。 編輯:預計變量的結果:8是較小的,因爲有更高的值比數組中的8。
出2:VAR B = [34,2,23,8] 預期輸出:較高,因爲所有其它的數字比一個[0]
如果數組中的第一個值高於或低於其他值,推薦使用哪種方法進行比較。將數組中的第一個值與其他值進行比較
我有一個數組如下面
VAR一個= [8,3,114,34,0,2]
我想比較,如果值A [0]是高於或低於其他數組值通過js。 編輯:預計變量的結果:8是較小的,因爲有更高的值比數組中的8。
出2:VAR B = [34,2,23,8] 預期輸出:較高,因爲所有其它的數字比一個[0]
的最佳方式下將在1開始一個for循環。
for(i = 1; i < a.length;i++){
if(a[0] > a[i])
{
//do something
}
else if(a[0] < a[i])
{
//do something
}
}
@DawnPatrol如果他遵循你的建議,他將嘗試訪問一個不存在的數組元素,即[a.length]。這個解決方案沒有問題。他從i = 1開始,因爲他不需要比較第一個元素和它自己。 –
好點,評論刪除。 – DawnPatrol
如果你想知道,如果它比所有其他值比您可以撥打最小和最大功能類似下面的
var min = Math.min.apply(null, a);
var max = Math.max.apply(null, a);
測試嚴格之外的其他所有值更高或更低質量以及...
var a = [8,3, 114,34,0,2];
a.forEach(function(element) {
element === a[0] ? console.log (element + ' is equal to ' + a[0]) :
element > a[0] ? console.log(element + ' is higher than ' + a[0]) :
console.log(element + " is lower than " + a[0]);
});
//"8 is equal to 8"
//"3 is lower than 8"
//"114 is higher than 8"
//"34 is higher than 8"
//"0 is lower than 8"
//"2 is lower than 8"
// Create an array of -1/0/+1 for each value relative to first elt.
const comps = ([head, ...tail]) => tail.map(e => e < head ? -1 : e === head ? 0 : +1);
// Define some little convenience routines.
const greater = c => c === +1;
const less = c => c === -1;
// See if some or all element(s) are greater or less.
const someGreater = a => comps(a).some(greater);
const someLess = a => comps(a).some(less);
const allGreater = a => comps(a).every(greater);
const allLess = a => comps(a).every(less);
// Test.
const input = [8,3, 114,34,0,2];
console.log("Some are greater", someGreater(input));
console.log("Some are less", someLess(input));
console.log("All are greater", allGreater(input));
console.log("All are less", allLess(input));
一個有趣的絕招:
function first_is_bigger (array) {
var comp = array.join(" && " + array[0] + " > ");
return Function("return 1 | " + comp + ";")();
}
first_is_bigger([0, 1, 2]) // false
first_is_bigger([0, -1, -2]) // true
說明:
array = [1, 2, 3];
comp = array.join(" && " + array[0] + " > ");
// comp = "1 && 1 > 2 && 1 > 3"
exec_comp = Function("return " + comp + ";");
// exec_comp = function() { return 1 && 1 > 2 && 1 > 3; }
exec_comp()
// false
問題:0 && anything
總是false
:
array = [0, -1, -2]
comp = array.join(" && " + array[0] + " > ");
// comp = "0 && 0 > -1 && 0 > -2"
exec_comp = Function("return " + comp + ";");
// exec_comp = function() { return 0 && 0 > -1 && 0 > -2; }
exec_comp()
// false :-(
修正:1 | anything
總是不同於0
:
exec_comp = Function("return 1 | " + comp + ";");
// exec_comp = function() { return 1 | 0 && 0 > -1 && 0 > -2; }
exec_comp()
// true :-)
警告:使用不當動態評價的打開了你的代碼:-(
注入攻擊。根據我的問題的理解,我們打算檢查一個給定的元素是否爲數組的最大值(在我們的具體情況下是第一個)。我已經實施了一個更通用的function
,您可以在其中檢查任何元素,但index
0是默認值。
function isHigher(input, index) {
if (index === undefined) {
index = 0;
}
for (var i in input) {
if ((i !== index) && (input[i] > input[index])) {
return false;
}
}
return true;
}
呼叫isHigher(a)
檢查第0元素是否是最大的。如果你想檢查第五個元素,請致電isHigher(a, 5)
使用'for'循環遍歷值。然後'如果'與其他項目進行比較。 – zerkms
同意@zerkms,因爲您的時間複雜度不能低於N,因爲您需要至少檢查一次每個值以進行比較。 – Devesh
難道它不會高於某些值,低於其他值並等於其他值?或者你的意思是它是高於還是低於全部。例如,你想要的輸出是更高,更低,更高,更高? –