javascript - Map null values to array -


why isn't null added keys array below?

html

<input type="radio" data-codekey="1"/> <input type="radio" data-codekey="null"/> 

js

$(function(){    var keys = $step.find('input[type="radio"]').map(function (i, el) {          return $(el).data('codekey');    }).get();  console.log(keys); // [1] //desired result: [1, null]  }); 

http://jsfiddle.net/mewem/

from the .map() documentation:

if function returns null or undefined, no element inserted.

the .data() function type conversion , (apparently) assumes string "null" should converted value null. converts string"1" number 1.

use .attr() instead of .data() , you'll actual string "null":

return $(el).attr('data-codekey'); 

if want actual null can't use .map(), you'd have rewrite .each() loop or something:

var keys = []; $step.find('input[type="radio"]').each(function () {     keys.push( $(this).data('codekey') ); }); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -