gpt4 book ai didi

javascript - 有什么办法可以干燥这段代码吗? + "toLowerCase()"

转载 作者:行者123 更新时间:2023-12-03 06:02:47 24 4
gpt4 key购买 nike

我对 JavaScript 还很陌生。我需要两件事:

  1. 干燥我的代码。具体来说,{leo} 对象的 hasMoreOscarsThan 方法内部声明了很多变量。我需要在对象 {jenn}、{sam}、{mery} 和 {< 的其他 4 个方法中以某种方式再次使用这些变量强>约翰}。

将这些变量声明为全局变量根本行不通,因为我什至不想稍后更改它们的值。

在其他对象的其他 hasMoreOscarsThan 方法中使用这些变量时,有什么方法可以干燥这些变量吗?

  • 我有 getActorByName() 函数。我无法在其中使用 toLowerCase()toUpperCase() 函数。为什么?
  • 如您所知,例如,用户通常不会尝试完全按照大写字母和小写字母书写名字“Leonardo DiCaprio”。 toLowerCase() 可以为我解决问题,但我无法让它工作!为什么?

    提前非常感谢您!

    var actors = [
    leo = {
    name: 'Leonardo DiCaprio',
    age: 41,
    oscars: 1,
    hello: function() {
    var logActorName0 = console.log('Hello, my name is: ' + leo.name);


    // hasMoreOscarsThan - this method accepts one actor object as a parameter and
    // returns true if the actor has more Oscars than the one that is passed as
    // a parameter and false otherwise.


    },
    hasMoreOscarsThan: function(actorObj) {
    var leoName = actors[0].name;
    var jennName = actors[1].name;
    var samName = actors[2].name;
    var meryName = actors[3].name;
    var johnName = actors[4].name;
    var leoOscars = actors[0].oscars;
    var jennOscars = actors[1].oscars;
    var samOscars = actors[2].oscars;
    var meryOscars = actors[3].oscars;
    var johnOscars = actors[4].oscars;
    if (actorObj === leoName && leoOscars > leoOscars) {
    return true;
    } else if (actorObj === jennName && leoOscars > jennOscars) {
    return true;
    } else if (actorObj === samName && leoOscars > samOscars) {
    return true;
    } else if (actorObj === meryName && leoOscars > meryOscars) {
    return true;
    } else if (actorObj === johnName && leoOscars > johnOscars) {
    return true;
    } else if (actorObj !== leoName && actorObj !== jennName && actorObj !== samName && actorObj !== meryName && actorObj !== johnName) {
    var falseSearch = "Please enter a valid actor name!";
    return falseSearch;
    } else {
    return false;
    }
    }
    },
    jenn = {
    name: 'Jennifer Lawrence',
    age: 25,
    oscars: 1,
    hello: function() {
    var logActorName1 = console.log('Hello, my name is: ' + jenn.name);
    },
    hasMoreOscarsThan: function(actorObj) {
    console.log(leoname);
    }
    },

    sam = {
    name: 'Samuel L. Jackson',
    age: 67,
    oscars: 0,
    hello: function() {
    var logActorName2 = console.log('Hello, my name is: ' + sam.name);
    },
    hasMoreOscarsThan: function(actorObj) {

    }
    },
    mery = {
    name: 'Meryl Streep',
    age: 66,
    oscars: 3,
    hello: function() {
    var logActorName3 = console.log('Hello, my name is: ' + mery.name);
    },
    hasMoreOscarsThan: function(actorObj) {

    }
    },

    john = {
    name: 'John Cho',
    age: 43,
    oscars: 0,
    hello: function() {
    var logActorName4 = console.log('Hello, my name is: ' + john.name);
    },
    hasMoreOscarsThan: function(actorObj) {

    }
    },


    function getActorByName(actor){
    var leoName = actors[0].name;
    var jennName = actors[1].name;
    var samName = actors[2].name;
    var meryName = actors[3].name;
    var johnName = actors[4].name;
    if (actor === leoName){
    return (leo);
    } else if (actor === jennName){
    return (jenn);
    } else if (actor === samName){
    return (sam);
    } else if (actor === meryName){
    return (mery);
    } else if (actor === johnName){
    return (john);
    } else if (actor !== leoName && actor !== jennName && actor !== samName && actor !== meryName && actor !== johnName){
    return "No Such An Actor Found!";
    }
    };
    <!doctype html>
    <html>
    <title>Actors</title>
    <script src="script.js"></script>

    </html>

    最佳答案

    我想您可以使用单个过滤器操作来完成此操作,如下所示;

    var hasMoreOscars = (o,a) => a.filter(e => e.oscarCount < o.oscarCount),
    arr = [{a:"leo",oscarCount:1},{a:"me",oscarCount:10},{a:"John", oscarCount: 3}, {a:"she",oscarCount:7}];

    console.log(hasMoreOscars(arr[3],arr))

    关于javascript - 有什么办法可以干燥这段代码吗? + "toLowerCase()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39686351/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com