gpt4 book ai didi

javascript二进制操作不适用于选项

转载 作者:行者123 更新时间:2023-11-30 16:59:31 24 4
gpt4 key购买 nike

这是我的...某些数组项具有二进制选项喜欢

  • $item[0] = 0010
  • $item[1] = 1000
  • $item[2] = 0110
  • $item[3] = 1101

每一位代表一些选项

我需要的是将其与客户请求选项进行比较假设它是 0010

所以我只需要显示 $item[0] 和 $item[2] 的逻辑,因为第二个字节是 1。但是,当没有客户选项时检查:0000 我必须全部显示它们

只有在显示某些选项时才必须有过滤器...

我应该在我的数学课上多听...我现在一无所知,请帮忙!


注意:一切都来自这里:http://jsfiddle.net/yHxue/但我不明白这一行:markers[m].setMap(((markers[m].props & props)>>>0===props)? ((props)?map:null): null);, 所以我重写了它,我的不起作用!

最佳答案

如果您的数据是实际数字,则只需对每个成员使用按位 & 运算符即可。

var customerOption = parseInt("0010", 2)

$item.forEach(function(n, i) {
if (customerOption === 0 || ((n & customerOption) == customerOption))
alert(i); // show this item
});

如果它们是字符串,那么您需要先将每个项目转换为数字。

同上,但是...

parseInt(n, 2);

var $item = [];
$item[0] = "0010"
$item[1] = "1000"
$item[2] = "0110"
$item[3] = "1101"

var customerOption = parseInt("0010", 2)

$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0010 matched index: " + i;
});

document.querySelector("pre").textContent += "\n\n"

var customerOption = parseInt("0110", 2)

$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0110 matched index: " + i;
});
<pre></pre>

关于javascript二进制操作不适用于选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136052/

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