有没有更好的方法让这个函数如预期般返回true?

huangapple go评论49阅读模式
英文:

Is there a better way to make this function return true as expected

问题

我有一个JS逻辑,我正在尝试实现,现在正在思考为什么它不起作用:我有两个数组,它们至少有一个相似的元素,但实现的逻辑返回了false - 但我期望的结果是相反的。请看一下:

 const array_one = [
  "Apparel",
  "Footwear",
];

const array_two = [
  "Soap",
  "Footwear",
];

const checkArray = (arr1, arr2) => {
    if (arr1.length !== arr2.length) {
      return false;
    } else {
      for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] !== arr2[i]) {
          return false;
        }
      }
    }
    return true;
  };

  console.log(checkArray(array_one, array_two)); //将`false`记录到控制台,期望为'true'。

希望这对你有帮助。

英文:

I have a JS logic that I am trying to implement, and now pondering why it is not working: I have two arrays with at least one similar element in them, but implementing the logic returned false - but I was expecting the opposite result. Please take a look:

 const array_one = [
  &quot;Apparel&quot;,
  &quot;Footwear&quot;,
];

const array_two = [
  &quot;Soap&quot;,
  &quot;Footwear&quot;,
];


const checkArray = (arr1, arr2) =&gt; {
    if (arr1.length !== arr2.length) {
      return false;
    } else {
      for (let i = 0; i &lt; arr1.length; i++) {
        if (arr1[i] !== arr2[i]) {
          return false;
        }
      }
    }
    return true;
  };

  console.log(checkArray(array_one, array_two)); //logs false to the console, was expecting &#39;true&#39;.

答案1

得分: 3

  1. 使用 some 方法来检查一个数组中是否存在另一个数组的任何值:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.some(value => arr2.includes(value));
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. 使用 filter 方法:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.filter(value => arr2.includes(value)).length > 0;
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. 使用 Setsome 方法,这更高效,因为 Sethas 方法的平均时间复杂度为 O(n),而且不需要遍历整个数组:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    const set = new Set(arr2);
    return arr1.some(value => set.has(value));
  }
};

console.log(checkArray(array_one, array_two)); //true
  1. 使用 reduce 方法:
const array_one = ["Apparel", "Footwear", ];
const array_two = ["Soap", "Footwear", ];

const checkArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.reduce((accumulator, value) => accumulator || arr2.includes(value), false);
  }
};

console.log(checkArray(array_one, array_two)); //true

注意:方法1. filter 和 2. reduce 不太高效,因为它们创建新数组并累积值,这可能对较大的数组来说会占用内存并变慢。

英文:
  1. Using some method to check if any value from one array exists in another array:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array_one = [ &quot;Apparel&quot;, &quot;Footwear&quot;, ]; const array_two = [ &quot;Soap&quot;, &quot;Footwear&quot;, ];
const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.some(value =&gt; arr2.includes(value));
  }
};

console.log(checkArray(array_one, array_two)); //true

<!-- end snippet -->

  1. Using filter method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array_one = [ &quot;Apparel&quot;, &quot;Footwear&quot;, ]; const array_two = [ &quot;Soap&quot;, &quot;Footwear&quot;, ];
const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.filter(value =&gt; arr2.includes(value)).length &gt; 0;
  }
};

console.log(checkArray(array_one, array_two)); //true

<!-- end snippet -->

  1. Using the some method with the Set which is more performant since it has an average time complexity of O(n) for the has method, and does not require iterating through the entire array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array_one = [ &quot;Apparel&quot;, &quot;Footwear&quot;, ]; const array_two = [ &quot;Soap&quot;, &quot;Footwear&quot;, ];

const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    const set = new Set(arr2);
    return arr1.some(value =&gt; set.has(value));
  }
};

console.log(checkArray(array_one, array_two)); //true

<!-- end snippet -->

  1. Using the reduce method:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array_one = [ &quot;Apparel&quot;, &quot;Footwear&quot;, ]; const array_two = [ &quot;Soap&quot;, &quot;Footwear&quot;, ];

const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false;
  } else {
    return arr1.reduce((accumulator, value) =&gt; accumulator || arr2.includes(value), false);
  }
};

console.log(checkArray(array_one, array_two)); //true

<!-- end snippet -->

Note: methods 1. filter and 2. reduce are less performant , since they create new arrays, accumulate values, which can be memory-intensive and slow for larger arrays.

答案2

得分: 0

只需将逻辑颠倒过来。

  • false设为默认假设
  • 为每个输入Array创建一个Set,以便进行O(1)查找
  • 检查集合中当前项目是否存在于另一个集合中,如果是,则立即返回true
英文:

Just flip your logic around.

  • Make false the default assumption
  • Create a Set for each input Array for O(1) lookup
  • Check if the current item in the set exists in the other and immediately return true

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
  array_one   = [ &quot;Apparel&quot; , &quot;Footwear&quot; ],
  array_two   = [ &quot;Soap&quot;    , &quot;Footwear&quot; ],
  array_three = [ &quot;Apple&quot;   , &quot;Orange&quot;   ];

const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false; // lengths must match
  }
  const
    set1 = new Set(arr1),
    set2 = new Set(arr2);
  for (const [value] of set1.entries()) {
    if (set2.has(value)) {
      return true; // short-circuit with true
    }
  }
  return false; // default assumption
};

console.log(checkArray(array_one, array_two));   // true
console.log(checkArray(array_one, array_three)); // false

<!-- end snippet -->

If you think Set is overkill, you can always use Array.prototype.includes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
  array_one   = [ &quot;Apparel&quot; , &quot;Footwear&quot; ],
  array_two   = [ &quot;Soap&quot;    , &quot;Footwear&quot; ],
  array_three = [ &quot;Apple&quot;   , &quot;Orange&quot;   ];

const checkArray = (arr1, arr2) =&gt; {
  if (arr1.length !== arr2.length) {
    return false; // lengths must match
  }
  for (let i = 0; i &lt; arr1.length; i++) {
    if (arr2.includes(arr1[i])) {
      return true; // short-circuit with true
    }
  };
  return false; // default assumption
};

console.log(checkArray(array_one, array_two));   // true
console.log(checkArray(array_one, array_three)); // false

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 03:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定