I got this little piece of code which is used for a google maps.
To 'link' the markers to a filter there is a count.
Each marker has a props which holds a number so it will be able to filter.

at the minute the math.pow is used.
You understand the props for the markers go from 1-2-4-8-16-32 untill you get over millions and billions. What i'd like to do is alter the code so it does 1+1=2 1+2=3 4, 5 ,6 ,7 and so on so i don't have to fill in these HUGE numbers.

The code is here:

$.each(sizer,function(i,b){
    props+=($(b).is(':checked'))?Math.pow(2,i):0;
});
有帮助吗?

解决方案

If I understand correctly, what you want is this:

$.each(sizer,function(i,b){
    props+=($(b).is(':checked')) ? i : 0;
});

There are two things for you to understand:

  • the syntax of the ternary operator is as follows: condition ? expr1 : expr2. Basicly it is a one-line if/else statement - if the condition is true - execute the first expression, otherwise execute the second expression. You can read more about it on the MDN docs

  • Math.pow is a built-in javascript function that accepts two numbers - a number and a power of this number. So Math.pow(2, i) means 2 to the power of i. This is something you probably guessed by now.

It is very hard to help more than that, because I don't have the rest of the code at my disposal. For example, how can I know what are the values of sizer? Etc...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top