简体   繁体   中英

Subtract a number from a variable

I'm trying to subtract from revlength each time the loop occurs. eg if there are 2 'A Google user' posts then subtract 2 from revlength

this is what I tried.

var revlength = place.reviews.length;
if(place.reviews[index].author_name == 'A Google User') {
    $("#revnum").html(revlength--);
} 

upon adding to the script, the code does not work for

$.each(place.reviews, function (index, value) {
        if (place.reviews[index].text.length != 0) {
            if (place.reviews[index].text.length == 0) {
                $("#revnum").html(--place.reviews.length);
            }
            if (place.reviews[index].author_name == 'A Google User') {
                $("#revnum").html(--place.reviews.length);
                var imgid = '<img src="img/gplus.gif" width="50" height="50">';
            } else {
                $("#revnum").html(revlength);
                var imgid = '<img src="img/user.gif" width="50" height="50">';
            }

try this..

change

$("#revnum").html(revlength--);//you are doing post-decrement

to

$("#revnum").html(--revlength);//you need to change it pre-decrement.

If other parts are going right , then it will work sure.

Explanation:

--i and i-- where the position of the -- determines whether one is subtracted before or after the value is used.

if i-- the value will be used before decremented. if --i the value will be used after decremented.

Turning my comment (that appears to have worked) into an answer:

You need to actually decrement place.reviews.length .

if(place.reviews[index].author_name == 'A Google User') {
    $("#revnum").html(place.reviews.length--);
} 

Your original code was only decrementing a temporary variable revlength which was not persistent because you would just reload that variable from place.reviews.length each time. Thus place.reviews.length never got decremented.


By way of explanation, perhaps you were under the impression that after doing:

var revlength = place.reviews.length; 

That when you then did:

revlength--

that it would also decrement place.reviews.length , but that is not the case. revlength is a new variable that was set to have the value of place.reviews.length , but when you do revlength-- , that only changes that variable and doesn't have any effect on place.reviews.length . So, for the decremented value to persist, you need to decrement place.reviews.length .

I think this is what you want:

var revlength = $.grep(place.reviews, function (val) {
    return val.author_name !== 'A Google User';
}).length;
// alert(revlength);
// console.log(revlength);

Try this one,

var revlength = place.reviews.length;
revlength =  parseInt(revlength.toString());
if(place.reviews[index].author_name == 'A Google User') {
revlength--;
$("#revnum").html(revlength);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM