javascript - How do I check if a property exists in an object/dictionary? -
i'm iterating on array of words , trying stuff them in object literal can assign value of how many times words occur each word in literal/dictionary. problem need check make sure word hasn't been added literal. tried using in
check if property exists in literal it's throwing error:
cannot use 'in' operator search 'we' in undefined
here's problematic function:
i commented line that's causing problem
function wordcountdict(filename) { wordcount = {}; inputfile = fs.readfile( root + filename, 'utf8', function( error, data ) { if(error) { console.log('error: ', error) return false; } var words = data.split(" "); (i in words) { if(words[i] in wordcount) { // problem occurs wordcount[words[i]]++; } else { wordcount[words[i]] = 1; } console.log(words[i]); } }); }
i'm coming python , best/easiest way achieve this, javascript doesn't seem agree.
how in javascript?
declare wordcount
local variable function. getting overwritten elsewhere:
function wordcountdict(filename) { var wordcount = {}; ... }
Comments
Post a Comment