我正在嘗試創建一個將陣列作為輸入和目標的函式,然后迭代陣列以檢查當前索引是否等于目標。如果是這樣,它將從陣列中拼接當前索引。
到目前為止一切正常,但是當我實作一個 if 陳述句來檢查索引是否在陣列的末尾,然后檢查結果陣列是否等于輸入陣列。我真的不知道為什么這花了我這么長時間,這有點尷尬......這是我的代碼:
let array = ['fox', 'tiger', 'elephant', 'jaguar', 'wolf', 'deer', 'hog', 'dhole', 'leopard', 'eagle', 'bear'];
const splice = (arr, target) => {
//creates empty result array
let result = [];
//iterate through the input array and push each item to the result array
for(let i = 0; i < arr.length; i ) {
result.push(arr[i]);
}
let j = 0;
//iterate through result array
while(j < result.length) {
if (result[j] === target) {
result.splice(j, 1);
}
//i have tried this multiple times with the if statement in and out of the loop
if (j === result.length && result === arr) {
//throw new Error(`${target} was not found in the array`);
console.log({result, arr, j});
return 'equal';
} else if (j === result.length && result !== arr ) {
console.log({result, arr, j});
return 'different';
}
j ;
}
};
//should return 'equal' but returns 'different'
console.log(splice(array, 'turtle'));
//should return 'different' but returns undefined
console.log(splice(array, 'bear'));
uj5u.com熱心網友回復:
希望這有幫助
const splice = (arr, target) => {
//creates empty result array
let result = [];
//iterate through the input array and push each item to the result array
for(let i = 0; i < arr.length; i ) {
result.push(arr[i]);
}
let j = 0;
//iterate through result array
while(j < result.length) {
if (result[j] === target) {
result.splice(j, 1);
/* since you modified the length of result array here,
u can simply check the length of result array
with the original array to see if there's a different */
}
// result === arr will always resolve to fasle as they are not the same object, so check the length instead
if (j === result.length && result.length !== arr.length) {
//throw new Error(`${target} was not found in the array`);
console.log({result, arr, j});
return 'equal';
} else if (j === result.length && result.length === arr.length ) {
console.log({result, arr, j});
return 'different';
}
j ;
}
// a better way of doing it is to move the if check outside of while loop to avoid it run multiple time
if (result.length !== arr.length) { // different length, that mean we found the target and the result array got modified
console.log({result, arr, j});
return 'equal';
} else {
console.log({result, arr, j});
return 'different';
}
};
uj5u.com熱心網友回復:
在這里,您可以使用此邏輯來比較兩個陣列是否相同:
let arr1 = [1, 2, 3, -2, null];
let arr2 = [1, 2, 3, -2, null];
let bool = true;
let i;
for (i = 0; i < arr1.length; i ) {
if (arr1[i] !== arr2[i]) {
bool = false;
break;
}
}
if (arr1[i] === arr2[i]) bool = true;
if (arr1[i] !== arr2[i] || arr1.length !== arr2.length) bool = false;
if (bool) console.log("same");
else console.log("different");
uj5u.com熱心網友回復:
正如其他人在評論中告訴您的那樣,由于陣列是 JavaScript 中的物件,因此您無法將它們與簡單的 === 進行比較。
要比較 2 個陣列,首先,您必須確定 arr1 === arr2 是什么意思。
例如:[A,C,B] concider 是否等于 [A,B,C]?[A,B,C,]怎么樣?
假設您需要兩個陣列...
1.) 相同的長度,
2.) 索引值必須匹配。
你可以這樣做:
const arr1 = [1,2,3,"A"];
const arr2 = [1,2,3,"A"];
function compareArr(A,B) {
return (A.length === B.length) && A.every((v,i) => B[i] === v)
}
console.log(compareArr(arr1,arr2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/532954.html
