我想使用javascript的reduce()方法获取字符串上元音的数量.下面是代码,问题是在解构acc之后a,e,i,o,u的值是未定义的.
const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';
const strArr = str.split('');
const mapVowels = strArr.reduce(countVowels, {});
function countVowels(acc, char) {
console.log(char)
var { a = 0, e = 0, i = 0, o = 0, u = 0 } = acc;
if (char === "a") {
return {...acc, a: a + 1};
}
if (char === 'i') {
return { ...acc, …Run Code Online (Sandbox Code Playgroud) 我有一个名称输入字段,其中包含所有字符,包括空格。我想使用正则表达式不允许第一个字符为空格。用户必须输入至少一个有效字符才能输入空格。谢谢你的帮助。
我有一个反应组件和该组件的某些部分,该部分仅根据条件呈现。当我单击组件(按钮)的可见部分时,它调用状态更改并呈现隐藏部分。现在我想淡出隐藏的部分,而不是立即显示。有没有办法只使用 CSS 而不使用任何外部库(如 CSSTranstionGroup)来做到这一点?感谢您的帮助。
import React, { Component } from "react";
export default class Parent extends Component {
state = {
showForm: false
};
render() {
const { showForm } = this.state;
return (
<div>
<button onClick={() => this.setState({ showForm: true })}>
Fade in form on click
</button>
{showForm && (
<div>
<h2>
This part needs to be fade in once the **showForm** is true
</h2>
<form></form>
</div>
)}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)