为什么map不返回一个新数组但返回以逗号分隔的字符串

时间:2018-01-01 21:19:43

标签: javascript

Answers.js

const { ups , answer, displayName, photoURL, userid, increment, answerid, index, answers} = props;

  const upvote = () => {
  const newarr = answers.map(item => item.voters);
  console.log("new arr is " + newarr) 

// console记录由逗号分隔的字符串而不是选民数组

  authUser &&  increment(index);
}

app.js

increment = (index) => {
  let answers = this.state.answers.slice();
  ++answers[index].ups;
  answers[index].voters.push(answers[index].userid)
  this.setState({answers})
}

这是答案的样子

makeAnswer() {
  const answer = ({ answer:this.state.answer, belongsTo:this.state.openedPost, ups:0, isAccepted: false, id:this.getPostId(), userid:this.context.authUser.uid, displayName:this.context.authUser.displayName, photoURL:this.context.authUser.photoURL, date:Date.now(), voters:[] })
  return answer;
}

1 个答案:

答案 0 :(得分:2)

由于连接,您的newarr会转换为字符串;这样做:

console.log("new arr is", newarr)