javascript 如何修复无效的解构分配目标?

5uzkadbs  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(120)

我可以做些什么来解决我的问题?我是一个新的JavaScript新手和任何recomendations或建议可能对我有帮助。

var user = {
    username: "Andrey",
    password: "JavaScript"
    },
    {
    username: "Max",
    password: "12345"
    },
    {
    username: "Pasha",
    password: "OWL"
    };

var database = [user];

var newsfeed = [
    {
        username: "Bobby",
        timeline: "DOOOOOOG!",
    },
    {
        username: "Max",
        timeline: "CAAAAT!",
    },
    {
        username: "Lida",
        timeline: "John Ceeeenaaaa!",
    }
];

var userNamePrompt = prompt("Your Username?");
var passwordPrompt = prompt("Your password?");

function isUserValid(Name, Pass){
    for (var i=0; i<database.length; i++){
        if (database[i].username === Name &&
            database[i].password === Pass) {
            return true;
        }
    }
    return false;   
}

function SignIn (Name, Pass){
    if (isUserValid(Name, Pass)) {
        console.log(newsfeed);
    }
    else {
        alert("Sorry smt went wrong!");
    }
}

SignIn (userNamePrompt, passwordPrompt);

如果代码工作正常,我应该得到一个带有newsfeed的数组,但我得到的是:无效的解构分配目标

gt0wga4j

gt0wga4j1#

你写的是:var user = { property }{ property }{ property },它不起作用。这可能就是你的意思(也跳过了var database = [ user ];赋值):

var database = [
    {
      username: "Andrey",
      password: "JavaScript"
    },
    {
      username: "Max",
      password: "12345"
    },
    {
      username: "Pasha",
      password: "OWL"
    }
]

相关问题