1

What I'm trying to do is add a check to my for loop that searches for "value_1" or "value_3". If found, than add a "#" at the beginning of that value.

Ex: #value_1, value_2, #value_3, etc

Here's my code:

for(i=1; i < columns.length-1; i++){
    currentRecordKey = columns[i].dataIndex;
    if(currentRecordKey == "value_1" || "value_3") {
        currentRecordKey = "#" + currentRecordKey;
    }
}

Worked out in my head, but this doesn't get the job done.

Any ideas?

Cheers

3
  • What seems to be the issue? You haven't explained where your code goes wrong or what you're trying to fix. Commented Dec 12, 2013 at 20:18
  • "value_3" is alway truthy. Looks like you have a typo. Commented Dec 12, 2013 at 20:20
  • Sorry My post was riddled with typos haha. But the -1 is necessary for the grid that I have set up Commented Dec 12, 2013 at 20:22

1 Answer 1

4

It should be

for(i=1; i < columns.length-1; i++){
    //icon record
    var currentRecordKey = columns[i].dataIndex;
    if(currentRecordKey == "value_1" || currentRecordKey == "value_3") {
         currentRecordKey = "+" + currentRecordKey ;
    }
    columns[i].dataIndex = currentRecordKey;
}

Your error is that if you don't restate that you are checking for currentRecordKey == "value_3", JavaScript will only check if currentRecordKey is true, and any non-empty string is.

Also, I've commented out icon record which might also be a typo.

Edit: also fixed the issue explained by Jasper and made currentRecordKey a local variable.

Sign up to request clarification or add additional context in comments.

3 Comments

If you want to alter the data in the columns array then this won't do it, it's still saving the "updated" string in a separate variable which will be overwritten each iteration of the loop. But if you're looking to do work with that new variable within the loop, you should be good to go.
@Jasper: fixed the issue
Oh, no fix really needed I think. The OP hasn't stated what they're really after, nice answer though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.