How do I check if a property exists in an object/dictionary? - javascript

I'm iterating over an array of words and trying to stuff them in an object literal so I can assign the value of how many times those words occur to each word in the literal/dictionary. The problem is I need to check to make sure that word hasn't already been added into my literal. I tried using in to check if the property exists in the literal but it's throwing an error:
Cannot use 'in' operator to search for 'We' in undefined
Here's problematic function:
I commented the line that's causing the 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(" ");
for (i in words) {
if(words[i] in wordCount) { // This is where the problem occurs
wordCount[words[i]]++;
} else {
wordCount[words[i]] = 1;
}
console.log(words[i]);
}
});
}
I'm coming from python and this was always the best/easiest way to achieve this, but javascript doesn't seem to agree.
How would I do this in JavaScript?

Declare wordCount as a local variable to that function. It is probably getting overwritten elsewhere:
function wordCountDict(filename) {
var wordCount = {};
...
}

This is a bad idea
for (i in words) {
do not use a for loop to loop through an array! If something is added to the array prototype it will be checked.
var words = data.split(" ");
for (var i=0; i<words.length; i++) {
if(words[i] in wordCount) {
Next thing, is readFile is asynchronous. If code outide of it resets wordCount to an undefined value, you can get this error. You are better off using a local variable and setting the global value when the looping is done. Also that return false does NOTHING inside the readFile.
function wordCountDict(filename) {
var tempWordCount = {};
var inputFile = fs.readFile( root + filename, 'utf8', function( error, data ) {
if(error) {
console.log('error: ', error)
return false;
}
var words = data.split(" ");
for (var i = 0; i<words.length; i++) {
if(words[i] in wordCount) { // This is where the problem occurs
wordCount[words[i]]++;
} else {
wordCount[words[i]] = 1;
}
console.log(words[i]);
}
wordCount = tempWordCount; //set the global variable equal to the local value
});
}

If all you would like to do is check for existance in the object, you can use this:
if(typeof wordCount[words[i]] === 'undefined'){
...
}
I would not recommend just using if(wordCount[words[i]]) because technically there could be a property of the object that exists but evaluates to false.
Note that in Javascript doing something like myObject.something is equivalent to myObject['something'] on an object, and that when you use myObject['somethingElse'] you are basically just dynamically adding members to the object. In Javascript, objects can be used like Python dictionaries, but they really aren't the same thing.

Related

how to check the presence of the element in the array?

please help solve the problem.
live example is here: https://jsfiddle.net/oqc5Lw73/
i generate several tank objects:
var Tank = function(id) {
this.id = id;
Tank.tanks.push(this);
}
Tank.tanks = [];
for (var i = 0; i < 3; i++) {
new Tank(i);
}
Tank.tanks.forEach(function(tank, i, arr) {
console.log(tank);
});
console.log('summary tanks: ' + Tank.tanks.length);
after i delete tank with random index:
var tankDel = Math.floor(Math.random() * (3));
Tank.tanks.splice(tankDel, 1);
Tank.count -= 1;
Tank.tanks.forEach(function(tank, i, arr) {
console.log(tank);
});
console.log('summary tanks: ' + Tank.tanks.length);
i try check tanks massive. if tanks massive contain tank with property 'id' = 0 then i need display alert('tank with id 0 is dead').
but console output follow error message:
Uncaught SyntaxError: Illegal break statement
break is to break out of a loop like for, while, switch etc which you don't have here, you need to use return to break the execution flow of the current function and return to the caller. See similar post here: illegal use of break statement; javascript
Tank.tanks.forEach(function(tank, i, arr) {
if(tank.id == 0) {
tank0Dead = false;
return;
};
});
if(tank0Dead == true) {
alert('tank with id 0 is dead');
};
jsfiddle : https://jsfiddle.net/oqc5Lw73/6/
You can't quit from forEach using break. Just remove break, and it will work.
P.S: honestly, it is better to refactor that code:)
Your only problem is that you can't use the break; statement in a forEach function.
But you can in a for() loop, so here is the equivalent code with a for :
for (var i = 0; i < Tank.tanks.length; i++){
if (Tank.tanks[i].id == 0){
tank0Dead = false;
break;
}
}
https://jsfiddle.net/oqc5Lw73/5/
But I agree with #dimko1 about the idea of refactoring the code
You can not break a forEach callback, simply because it's a function.
Here's updated working jSfiddle
If you really want to break it, you can use exception like code below.
try {
[1,2,3].forEach(function () {
if(conditionMet) {
throw Error("breaking forEach");
}
});
} catch(e) {
}
Otherwise you can use jQuery's each() method. when it's callback returns false it stops.
jQuery.each([1,2,3], function () {
if(conditionMet) {
return false;
}
});

need help using for loop and getText() in protractor

browser.findElements(protractor.By.repeater('cat in cats')).then(function(rows) {
for (i = 0; i < rows.length; i++) { //which is always 3
var tmp = element(by.repeater('cat in cats').row(i)).element(by.binding('cat.name')).getText();
tmp.then(function(text) {
console.log('text is : ' + text);
console.log('iteration number is: ' + i);
if (text == 'someText') {
element(by.repeater('cat in cats').row(i)).element(by.binding('cat.action')).click();
}
});
}
In this case the value of 'i' inside the function is always returning 3.
I have get text and then check if the text is what I want and click on an element.
The value of 'i' in the 'if' statement is always returned as 3. Something to do with promises, but I am not sure.
Any help with modified code is much appreciated.
Thanks!
Don't call by.repeater() multiple times; instead, use map() and "chain" the promises like this:
element.all(By.repeater('cat in cats')).map(function(elm) {
return {
text: elm.element(by.binding('cat.name')).getText(),
action: elm.element(by.binding('cat.action'))
};
}).then(function(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i].text == 'someText') {
return arr[i].action;
}
}
throw new Error('Text not found');
}).then(function(elm) {
elm.click();
});
All of the credits go to #Andres for the solution provided here:
Passing Protractor ElementFinder to deferred.fulfill() results in a promise containing a null value
Also see:
Protractor find element inside a repeater

How to create a "function that returns a function and splits csv file"? [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 8 years ago.
I'm learning JS and I need help with the following task:
I need to create a function compile_csv_search(text, key_name) that parses text in
the CSV format. (not required to handle quoting and escaping in values;
assume field values never contain commas or other special characters.)
A function must return a function that looks up a record by a value of the
field specified as the second argument to compile_csv_search. Assume that all
values in the key field are unique.
Sample usage:
var csv_by_name = compile_csv_search(
"ip,name,desc\n"+
"1.94.0.2,server1,Main Server\n"+
"1.53.8.1,server2,Backup Server\n",
"name");
console.log(csv_by_name("server2"));
console.log(csv_by_name("server9"));
...will print:
{ip: "10.52.5.1", name: "server2", desc: "Backup Server"}
undefined
** I didn't understand what does it mean "function that return function". How can function return another function?
Thank you!
P.S.
attaching my solution for your review
function compile_csv_search(csvServerData){
var header = csvServerData.split('\n')[0].split(",");
var spleatedServerData = csvServerData.split('\n');
return function(serverName)
{
for(var i = 1; i < spleatedServerData.length; i++){
var singleServer = spleatedServerData[i].split(',')
var result = {};
var exist = false;
for (var j = 0; j < header.length; j++) {
if(singleServer.indexOf(serverName) == -1)
break;
exist = true;
result[header[j]] = singleServer[j];
}
if(exist){
return(result);
break;
}
}
}
}
var csv_by_name = compile_csv_search(
"ip,name,desc\n"+
"10.49.1.4,server1,Main Server\n"+
"10.52.5.1,server2,Backup Server\n");
Functions in JavaScript are objects; they can be referred to by variables, passed as arguments and returned from functions like any other object.
Here's a function that returns an object:
function returnObject() {
var result = { a: 1, b: 2, c: 3 };
return result;
}
And here's a function that returns another function:
function returnFunction() {
var result = function() {
console.log('another function!');
}
return result;
}
Notice how they're really similar - object returned by the first function is a plain Object created using object literal syntax ({}), and the object returned by the second happens to be a function.
You could call the inner, returned function like this:
var out = returnFunction();
out();
Or even returnFunction()();
However, you can't just call result() - result is only defined inside of returnFunction. The only way to access it from outside is to retrieve it by calling the outer function.
Something like this would be fine:
function compile_csv_search(text, key_name) {
var lines = text.split('\n');
var keys = lines[0].split(',');
var key_index = keys.indexOf(key_name);
return function(value) {
for(var i = 1; i<lines.length; i++) {
current_line_values = lines[i].split(',');
if(current_line_values[key_index] === value) {
var result = {};
for(var j = 0; j<keys.length; j++) {
result[keys[j]] = current_line_values[j];
}
return result;
}
}
}
}
Also see this fiddle: http://jsfiddle.net/efha0drq/
You can always treat a function the same as any other js objects. Assign to a variable, pass to a function, store in an array... all are fine.
The magic in this example is that, you can read/write the variables defined in the compile_csv_search() function within the returned function. So it's possible to store something in the local variables of the defining function, and later retrieve from the returned one, even when the defining function has finished execution long time ago. You may have heard of "closure", right?

Is it possible to use regexp as object property?

Let's say I have the following object:
var map = {};
map[/^\/e\/(?:([0-9a-f]{24}))\/rename\/?$/] = "/e/531e8942711f533cf0000019/rename";
The object above is mapping regex to testing value. I need to make something like the following:
for (var i in map) {
if (map.hasOwnProperty(i) && i.test(map[i])) {
console.log('It works');
}
}
But the code above doesn't work. What's wrong?
You can do:
var map = {};
map["^/e/(?:([0-9a-f]{24}))/rename/?$"] = "/e/531e8942711f533cf0000019/rename";
for (var i in map) {
if (map.hasOwnProperty(i) && (new RegExp(i)).test(map[i])) {
console.log('It works');
}
}
// prints: It works
Yes, you can; however, one thing to take note of is that regular expressions are first cast into a string before they're being used as a property name.
So when you want to use a property name as an expression again you must "rehydrate" it first:
for (var i in map) {
if (map.hasOwnProperty(i)) {
var re = eval(i);
if (re.test(map[i])) {
console.log('It works');
}
}
}
However, I would recommend using arrays instead to prevent this "magical" conversion to and fro:
var map = [];
map.push([/^\/e\/(?:([0-9a-f]{24}))\/rename\/?$/, "/e/531e8942711f533cf0000019/rename"]);
map.forEach(function(info) {
if (info[0].match(info[1])) {
console.log('it works');
}
});

How do I call a function stored in a variable that is of indeterminate depth

I looked at this:
Calling a JavaScript function named in a variable
But it doesn't answer my question.
This normally works:
window['class']['sub_class']['function_name'](data);
But now I'm trying to make a general function that can handle any depth:
function callbackFunction(callback, data){
//callback = a.b.c, or a.b, or a
callback = explode(callback);
//I need to be able to call callbackFunction and somehow callback and form their proper form below
window[callback.a](data);
//or
window[callback.a][callback.b](data);
//or
window[callback.a][callback.b][callback.c](data);
}
I believe the duplicate suggested by Bergi will only solve half of your problem. Since your final value will be a function, and since that function is a member of an object, you'll end up executing it in the wrong context (i.e., with the wrong this value).
I suggest you use something like this:
function getCallback(path) {
var arr = path.split('.');
var k;
var fn = window;
while(k = arr.shift()) {
if(typeof fn[k] === "function") {
fn = fn[k].bind(fn);
} else {
fn = fn[k];
}
}
if(typeof fn === "function") return fn;
return function(){};
}
http://jsfiddle.net/7CEd5/
Compare the value of this in the callback with what you get by using the answers to Convert string in dot notation to get the object reference.
You can chain references to objects/sub-objects/etc for however long you want. If you have a point-delimited string (e.g. "document.blah.blah2.method"), then you need to split it to individual tokens (e.g. ["document", "blah", "blah2", "method"]).
Then it's simply a matter of looping through the chain:
var c = window;
for (var i = 0; i < chain.length - 1; i++) {
c = c[chain[i]];
}
c[chain[chain.length-1]](some_arguments);

Categories

Resources