1

I have many js variables some of them is ok, some of them cant be set and makes them undefined, and undefined appears in html page, and this I don't want to happen.

How the js is now.

<script>
var externalData = 'data,data,data\n'
                 + 'data,data,data\n'
                 + 'data,data,data\n'
                 + 'data,data,data\n'
                 + 'data,data,data';
// split the string into rows, and split each row into cells
var parsed = externalData.split('\n').map(function(row) {
    return row.split(',');
});
</script>
</html>
<script>
// I try this but not working.
if ( parsed[4][6] !== undefined )
{
    var parsed[4][6] = null;
}
</script>
<script>document.write(parsed[0][0]);</script>
<script>document.write(parsed[4][6]);</script>

The is will output in the browser:

data undefined

The parsed[0][0] is ''data'' but the parsed[4][6] not exist in the table. well, I don't want the user to see that is undefined, but I want to see just nothing.

For example this:

data

instead of this

data undefined

I can remove undefined by just remove the document.write(parsed[4][6]); but I don't want to do this, I want to keep this in the page, and I don't want to modify the data-table above.

2
  • "underfunded" is perhaps my favourite autocorrect variation on "undefined"... Commented Aug 11, 2013 at 15:12
  • You have an alternative (and IMO better) solution than what you were trying to do below, but you could also do it the way you're trying to do it. There are two problems with the code you've pasted: you have parsed[4][6] !== undefined which should be parsed[4][6] === undefined, and the var at the start of the next line of code should also not be present. After making these changes, it should work. Commented Aug 11, 2013 at 15:37

1 Answer 1

1

If parsed[4][6] is either a string or undefined, you could simply use

parsed[4][6] || ''

(If parsed[4][6] can be something other than a string, e.g. a number, this will require a little extra care.)

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

Comments

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.