VBA中如何编写函数检查两个数组是否包含相同元素并返回布尔值?
实现判断两个数组/元组首元素是否相等的布尔返回函数
根据你提供的示例,函数的核心逻辑是判断两个数组(或元组)的第一个元素是否相等,相等则返回True,否则返回False。以下是主流编程语言的实现示例:
Python 实现
针对你给出的元组格式,Python实现如下(同时兼容列表):
def check_first_element_equal(first, second): # 先校验输入是否非空,避免索引越界报错 if not first or not second: return False return first[0] == second[0]
测试代码:
firstarray = ("haha", "ya", "test") secondarray = ("haha", "no", "stop") print(check_first_element_equal(firstarray, secondarray)) # 输出: True # 首元素不同的情况 thirdarray = ("hello", "a", "b") print(check_first_element_equal(firstarray, thirdarray)) # 输出: False # 空输入的情况 empty_tuple = () print(check_first_element_equal(firstarray, empty_tuple)) # 输出: False
JavaScript 实现
如果是在JavaScript环境中使用数组:
function checkFirstElementEqual(firstArr, secondArr) { // 校验数组非空 if (!firstArr.length || !secondArr.length) { return false; } return firstArr[0] === secondArr[0]; }
测试代码:
const firstarray = ["haha", "ya", "test"]; const secondarray = ["haha", "no", "stop"]; console.log(checkFirstElementEqual(firstarray, secondarray)); // 输出: true
内容的提问来源于stack exchange,提问作者OptionDisplay
相关产品推荐
相关产品推荐

