gpt4 book ai didi

javascript - 匹配两个javascript数组以形成交集结果

转载 作者:行者123 更新时间:2023-11-30 07:33:22 26 4
gpt4 key购买 nike

找到路口的简写和最佳方法是什么?

f = ["A","B","C","D","E","F"]; //might be less than 8
b = [1,0,0,1,0,0,0,0]; //always 8 elements

所需的结果数组 ["A","D"]

最佳答案

你可以使用 Array#filter

var f = ["A", "B", "C", "D", "E", "F"],
b = [1, 0, 0, 1, 0, 0, 0, 0],
r = f.filter((_, i) => b[i]);

console.log(r);

关于javascript - 匹配两个javascript数组以形成交集结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com