Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';动态分配JS对象属性_Javascript_Node.js_Express - Fatal编程技术网

Javascript Can';动态分配JS对象属性

Javascript Can';动态分配JS对象属性,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试制作一个web应用程序,它可以接收几个人的名字,并为每个人分配一个朋友,但在分配朋友时遇到了一个问题 我遇到的问题是,它正确地分配了前几个人的朋友,但总是在数组中倒数第二个人时崩溃,并显示错误消息 TypeError:无法设置未定义的属性“friend” 下面是我的app.jscode中的一段代码 const express = require("express"); const bodyParser = require("body-parser"

我正在尝试制作一个web应用程序,它可以接收几个人的名字,并为每个人分配一个朋友,但在分配朋友时遇到了一个问题

我遇到的问题是,它正确地分配了前几个人的朋友,但总是在数组中倒数第二个人时崩溃,并显示错误消息

TypeError:无法设置未定义的属性“friend”

下面是我的
app.js
code中的一段代码

const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.urlencoded({extended: true}));

let participantsObject = {};
let participantsArray = [];
let randomNumber;

app.get("/new", function(req, res) {
  res.sendFile(__dirname + "/new.html");
});

app.post("/new", function(req, res) {
  participantsObject[req.body.person] = {};
  participantsArray.push(req.body.person);
  res.redirect("/new");
});

app.get("/setup", function() {
  let size = participantsArray.length;
  let participantsWithoutAGifter = participantsArray;

  for (var i = 0; i < size; i++) {
    randomNumber = Math.floor(Math.random() * participantsWithoutAGifter.length)
    participantsObject[participantsArray[i]]["friend"] = participantsWithoutAGifter[randomNumber];
    participantsWithoutAGifter.splice(randomNumber, 1);
  }

  console.log(participantsObject);

});

app.listen(3000, function() {
  console.log("Server started on port 3000");
});
理想情况下,它将能够为所有参与者分配一个朋友,然后记录整个对象


非常感谢您的帮助。

循环试图选择的属性在
参与者对象上尚不存在

添加检查属性是否存在。如果是,则为对象指定
friend
属性。如果没有,则为新对象指定一个
friend
属性

for (var i = 0; i < size; i++) {
  randomNumber = Math.floor(Math.random() * participantsWithoutAGifter.length);
  let participantsIndex = participantsArray[i];
  if (participantsObject.hasOwnProperty(participantsIndex)) {
    participantsObject[participantsIndex]['friend'] = participantsWithoutAGifter[randomNumber]
  } else {
    participantsObject[participantsIndex] = {
      friend: participantsWithoutAGifter[randomNumber]
    }
  }
  participantsWithoutAGifter.splice(randomNumber, 1);
}
for(变量i=0;i

这里是指向代码的链接:

位于
participantsObject[participantsArray[i]]
的属性选择器返回未定义。请显示
participantsObject
participantsArray
是什么值。
participantsObject
participantsArray
是在有人提交与
new.html
participantsWithoutAGifter
关联的表单时填充的。
participantsArray
通过使用
splice
对该数组进行变异。参与循环的数组的突变可能是错误的来源。要么创建此数组的副本,要么不要在循环内对其进行变异
for (var i = 0; i < size; i++) {
  randomNumber = Math.floor(Math.random() * participantsWithoutAGifter.length);
  let participantsIndex = participantsArray[i];
  if (participantsObject.hasOwnProperty(participantsIndex)) {
    participantsObject[participantsIndex]['friend'] = participantsWithoutAGifter[randomNumber]
  } else {
    participantsObject[participantsIndex] = {
      friend: participantsWithoutAGifter[randomNumber]
    }
  }
  participantsWithoutAGifter.splice(randomNumber, 1);
}
// participantsObject[participantsArray[i]]["friend"] = participantsWithoutAGifter[randomNumber]; // <-- this will not work

const newProperty = 'friend'
participantsObject[participantsArray[i]] = {
  ...participantsObject[participantsArray[i]],  // <-- so that you don't lose other perperties
  [newProperty]: participantsWithoutAGifter[randomNumber]
}
const newProperty = 'friend'
const source = {}
source[newProperty] = participantsWithoutAGifter[randomNumber]
Object.assign(participantsObject[participantsArray[i]], source )