-2

Using:

<input id="selectedRecords" name="selectedRecords" type="hidden" />

Executing this code:

var keyValues = $('#selectedRecords').val().split(",").map(item => item.trim());

console.log('$(#selectedRecords).val(): [' + $('#selectedRecords').val() + ']');
console.log('keyValues: [' + keyValues + ']');
console.log('keyValues.length: [' + keyValues.length + ']');

Gives this console readout (copy pasted from console window in Brave browser):

$(#selectedRecords).val(): []
keyValues: []
keyValues.length: [1]

Why is keyValues.length returning a count of 1 ?

var keyValues = $('#selectedRecords').val().split(",").map(item => item.trim());

console.log('$(#selectedRecords).val(): [' + $('#selectedRecords').val() + ']');
console.log('keyValues: [' + keyValues + ']');
console.log('keyValues.length: [' + keyValues.length + ']');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="selectedRecords" name="selectedRecords" type="hidden" />

16
  • 3
    keyValues contains an array with one element, the empty string ''. Commented Nov 23, 2023 at 17:03
  • 1
    Your debugging approach is flawed and based on false assumptions. Instead of concatenating keyValues into strings in a variety of ways, log keyValues itself to the console and observe what it actually is. Commented Nov 23, 2023 at 17:05
  • 1
    .split(',') can't return an empty array. The only way for String.prototype.split to return an empty array is ''.split(''): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 23, 2023 at 17:09
  • 2
    It's closed because it's not useful for future visitors. It may be reproducible but so what? The question is "if I don't debug properly, why do I reach wrong conclusions" which is hardly something for the ages. Even if we don't go with the current close reason, this is a duplicate. The main dupe target would be Javascript console.log(object) vs. concatenating string then maybe a few others can be added. Commented Nov 24, 2023 at 10:38
  • @PaulZahra I was under the impression you did not understand the closure, so I explained the rationale. In the future, I shall endeavour to not do that, since you appear to consider the explanation "unkind". Commented Nov 27, 2023 at 11:04

1 Answer 1

2

You are concatenating things while logging them:

console.log('$(#selectedRecords).val(): [' + $('#selectedRecords').val() + ']');
console.log('keyValues: [' + keyValues + ']');
console.log('keyValues.length: [' + keyValues.length + ']');

keyValues is [""], but if you concatenate it with a string, its string representation will be used - the comma separated list of its values, in this case just "".

Solution: log things right!

console.log('$(#selectedRecords).val()', $('#selectedRecords').val());
console.log('keyValues', keyValues);
console.log('keyValues.length', keyValues.length);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.