JavaScript中的重复代码

时间:2017-01-06 16:04:07

标签: javascript duplicates

以下是如何在JS中删除重复代码以向类解释的一个很好的示例吗? 有人能给我一个更好的,可能触及其他角度以及改进这个角度的方法吗?

let employees = [{
    name: 'Paul',
    type: 'Manager'
  },
  {
    name: 'Rich',
    type: 'Developer'
  },
  {
    name: 'Rob',
    type: 'Cleaner'
  },
  {
    name: 'Chris',
    type: 'Developer'
  }]

var findManager = function(){
  employees.forEach(function(person){
    if (person.type == 'Manager') {
      console.log(person.name);
    }
  })
}
var findCleaner = function(){
  employees.forEach(function(person){
    if (person.type == 'Cleaner') {
      console.log(person.name);
    }
  })
}
var findDeveloper = function(){
  employees.forEach(function(person){
    if (person.type == 'Developer') {
      console.log(person.name);
    }
  })
}

// CODE REFACTORED BELOW

var findParticularEmployment = function(employment){
  employees.forEach(function(person){
    if (person.type == employment){
      console.log(person.name);
    }
  })
}

var findDeveloper = function(){
  return findParticularEmployment('Developer')
}

var findManager = function(){
  return findParticularEmployment('Manager')
}

var findCleaner = function(){
  return findParticularEmployment('Cleaner')
}

findManager()
findCleaner()
findDeveloper()

2 个答案:

答案 0 :(得分:0)

试试这个:

//retrieve quiz by id
    public static QuizDetails retrieveQuizDetailsById(int id) {
        // declare local variables
        QuizDetails quizdetails = null;
        ResultSet rs = null;
        DBController db = new DBController();
        String dbQuery;
        PreparedStatement pstmt;
        // step 1 - connect to database
        db.getConnection();

        // step 2 - declare the SQL statement
        dbQuery = "SELECT * FROM QuizDetails WHERE id=?";
        pstmt = db.getPreparedStatement(dbQuery);

        // step 3 - execute query
        try {
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            if (rs.next()) { // first record found
                quizdetails = convertToQuizDetails(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

        // step 4 - close connection
        db.terminate();
        return quizdetails;
        }


    private static QuizDetails convertToQuizDetails(ResultSet rs) throws SQLException {
        QuizDetails quizdetails;
        int id = rs.getInt("id");
        //String quizID = rs.getString("quizID");
        //int setNo = rs.getInt("setNo");
        int questionNo = rs.getInt("questionNo");
        String questionDesc = rs.getString("questionDesc");
        String option1 = rs.getString("option1");
        String option2 = rs.getString("option2");
        String option3 = rs.getString("option3");
        String option4 = rs.getString("option4");
        String correctAnswer = rs.getString("correctAnswer");



        quizdetails = new QuizDetails(id, questionNo, questionDesc, option1, option2, option3, option4, correctAnswer );

        return quizdetails;
   }


//Update
        public static boolean updateQuizDetails(QuizDetails quizdt) {
            //declare local variables
            boolean success = false;
            DBController db = new DBController();
            String dbQuery; 
            PreparedStatement pstmt;

            System.out.println("quiz details da ** 1a " + quizdt.getQuestionNo());
            System.out.println("quiz details da ** 1b " + quizdt.getQuestionDesc());

            //step 1 - establish connection to database
            db.getConnection();     

            //step 2 - declare the SQL statement
            //dbQuery = "UPDATE QuizDetails SET quizID = ?, setNo = ?, questionNo = ?, questionDesc = ?, option1 = ?, option2 = ?, option3 = ?, option4 =?, correctAnswer = ? WHERE id = ?";

            dbQuery = "UPDATE QuizDetails SET questionNo = ?, questionDesc = ?, option1 = ?, option2 = ?, option3 = ?, option4 =?, correctAnswer = ? WHERE id= ?";
            pstmt = db.getPreparedStatement(dbQuery);

            System.out.println("quiz details da 2 ** " + quizdt.getQuestionNo());

            //step 3 - to update record using executeUpdate method
            try {
                //pstmt.setInt(1, quizdt.getId());
                //pstmt.setString(1, quizdt.getQuizID());
                //pstmt.setInt(2, quizdt.getSetNo());
                pstmt.setInt(1, quizdt.getQuestionNo());
                pstmt.setString(2, quizdt.getQuestionDesc());
                pstmt.setString(3, quizdt.getOption1());
                pstmt.setString(4, quizdt.getOption2());
                pstmt.setString(5, quizdt.getOption3());
                pstmt.setString(6, quizdt.getOption4());
                pstmt.setString(7,quizdt.getCorrectAnswer());
                pstmt.setInt(8, quizdt.getId()); //Cannot be hard coded

                System.out.println(quizdt.getId());
                System.out.println("quiz details da 3 ** " + quizdt.getQuestionNo());

                if (pstmt.executeUpdate() == 1)
                    success = true;
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            //step 4 - close connection

            db.terminate();

            return success;     

        }

答案 1 :(得分:0)

您可以使用{type: [names]}创建一个对象,然后您可以使用type作为键来获取名称数组。



let employees = [{
  name: 'Paul',
  type: 'Manager'
}, {
  name: 'Rich',
  type: 'Developer'
}, {
  name: 'Rob',
  type: 'Cleaner'
}, {
  name: 'Chris',
  type: 'Developer'
}]

var obj = employees.reduce(function(r, e) {
  r[e.type] = (r[e.type] || []).concat(e.name)
  return r
}, {});

console.log(obj.Manager)
console.log(obj.Developer)