我们可以使用JavaScript中的循环和条件语句来完成此任务。具体实现,请参考以下代码示例:
const array1 = [1, 2, 3, 4, 5];
const array2 = [2, 4, 6, 8, 10];
const statusArray = [];
array1.forEach(element => {
if (array2.includes(element)) {
statusArray.push("存在");
} else {
statusArray.push("不存在");
}
});
console.log(statusArray); // 输出结果为:["存在", "存在", "不存在", "存在", "不存在"]
这里我们使用了array1
和array2
两个数组。我们首先使用forEach
循环来遍历array1
中的每一个元素。接着,我们使用includes
方法来检查array2
中是否存在该元素,如果存在,则将“存在”的状态添加到statusArray
中;否则,将“不存在”的状态添加到statusArray
中。最后,我们使用console.log()
方法来输出结果。
上一篇:比较两个数组,其中一些值是可选的