Iterating over every property of an object in javascript using Prototype?

55

Solution 1

You have to first convert your object literal to a Prototype Hash:

// Store your object literal
var obj = {foo: 1, bar: 2, barobj: {75: true, 76: false, 85: true}}

// Iterate like so.  The $H() construct creates a prototype-extended Hash.
$H(obj).each(function(pair){
  alert(pair.key);
  alert(pair.value);
});

Solution 2

There's no need for Prototype here: JavaScript has for..in loops. If you're not sure that no one messed with Object.prototype, check hasOwnProperty() as well, ie

for(var prop in obj) {
    if(obj.hasOwnProperty(prop))
        doSomethingWith(obj[prop]);
}
Share:
55
Çağdaş Kemik
Author by

Çağdaş Kemik

Updated on July 08, 2022

Comments

  • Çağdaş Kemik
    Çağdaş Kemik almost 2 years

    I'm trying to make something like a mini search engine right now. My goal is to index a bunch of files in a hashmap but first i need to perform a couple of operations, which include lowering capitals, removing all unnecessary words and also removing all characters except a-z/A-Z. Right now my implementation looks like this:

    String article = "";
    
    for (File file : dir.listFiles()) { //for each file (001.txt, 002.txt...)
            Scanner s = null;
            try {
                s = new Scanner(file);
                while (s.hasNext())
                    article += s.next().toLowerCase(Locale.ROOT) + " "; //converting all characters to lower case
                article = currentWord.replaceAll(delimiters.get()," "); //removing punctuations (?, -, !, * etc...) 
    
                String splittedWords = article.split(" ");  //splitting each word into a string array
                for(int i = 0; i < splittedWords.length; i++) {
                    s = new Scanner(stopwords);
                    boolean flag = true;
                    while(s.hasNextLine())
                        if (splittedWords[i].equals(s.nextLine())) { //comparing each word with all the stop words (words like a, the, already, these etc...) taken from another big txt file and removing them, because we dont need to fill our map with unnecessary words, to provide faster search times later on
                            flag = false;
                            break;
                        }
                    if(flag) map.put(splittedWords[i], file.getName()); //if current word in splittedWords array does not match any stop word, put it in the hashmap        
    
    
                }
                s.close();
    
    
            } catch (FileNotFoundException e) {
    
                e.printStackTrace();
            }
            s.close();
            System.out.println(file);
        }
    

    this is just a block from my code, it can contain missing pieces, i explained my algorithm in a cursory way with comments. Using .contains method to check if stopWords contains any currentWord, even though its a faster approach, it does not map words like "death" because it contains "at" from stop words list. Im trying to do my best to make it more effective but i haven't progressed much. each file containing approx. ~300 words each takes ~3 seconds to index which is not ideal considering i have ten thousands of files. Any ideas on how can i improve my algorithm to make it run faster?

    • Farvardin
      Farvardin over 4 years
      you are reading the stop-word file for each source file. you can read the stop-word file once and use a Set to store the stop words in memory.