javascript - ES6 includes but case insensitive - Stack Overflow

I know I can loop through array and just change value toLowerCase().I'm curious if there's a

I know I can loop through array and just change value toLowerCase().

I'm curious if there's a reactjs or es6 method that can check if the value is in array.

On addTodo function. You can see that I use includes but this method is case sensitive.

Here's what I have

class Todo extends React.Component {
    render() {
        return (
            <div className="todo">
                <input type="checkbox" />
                <p>{this.props.children}</p>
            </div>
        );
    }
}

class App extends React.Component {
    constructor(props) {
        super(props);

        this.todos = [
            'Get Up from bed', 
            'Eat Breakfast'
        ];
    }

    eachTodo(task, i) {
        return (
            <Todo key={i} index={i}>{task}</Todo>
        )
    }

    addToDo() {
        let task_title = this.refs.newTodo.value;

        if(task_title !== '') {
            let arr = this.todos;

            var arr_tlc = this.todos.map((value) => {
                return value.toLowerCase();
            })

            if(arr_tlc.indexOf(task_title.toLowerCase()) === -1) {
                arr.push(task_title);

                this.setState({
                    todos: arr
                });
            }
        }
    }

    render() {
        return (
            <div className="main-app">
                <input ref="newTodo" placeholder="Task"/>
                <button onClick={this.addToDo.bind(this)}>Add Todo</button>

                <div className="todos">
                    {this.todos.map(this.eachTodo)}
                </div>
            </div>

        );
    }   
}

Any help would be appreciated. Thanks!

I know I can loop through array and just change value toLowerCase().

I'm curious if there's a reactjs or es6 method that can check if the value is in array.

On addTodo function. You can see that I use includes but this method is case sensitive.

Here's what I have

class Todo extends React.Component {
    render() {
        return (
            <div className="todo">
                <input type="checkbox" />
                <p>{this.props.children}</p>
            </div>
        );
    }
}

class App extends React.Component {
    constructor(props) {
        super(props);

        this.todos = [
            'Get Up from bed', 
            'Eat Breakfast'
        ];
    }

    eachTodo(task, i) {
        return (
            <Todo key={i} index={i}>{task}</Todo>
        )
    }

    addToDo() {
        let task_title = this.refs.newTodo.value;

        if(task_title !== '') {
            let arr = this.todos;

            var arr_tlc = this.todos.map((value) => {
                return value.toLowerCase();
            })

            if(arr_tlc.indexOf(task_title.toLowerCase()) === -1) {
                arr.push(task_title);

                this.setState({
                    todos: arr
                });
            }
        }
    }

    render() {
        return (
            <div className="main-app">
                <input ref="newTodo" placeholder="Task"/>
                <button onClick={this.addToDo.bind(this)}>Add Todo</button>

                <div className="todos">
                    {this.todos.map(this.eachTodo)}
                </div>
            </div>

        );
    }   
}

Any help would be appreciated. Thanks!

Share Improve this question edited Nov 11, 2016 at 8:54 Kevin Durant asked Nov 11, 2016 at 8:23 Kevin DurantKevin Durant 731 silver badge5 bronze badges 5
  • Is the order in the array of importance? – trincot Commented Nov 11, 2016 at 9:18
  • @trincot No. I just need to check. – Kevin Durant Commented Nov 11, 2016 at 9:33
  • OK in that case I would suggest using a different data structure for your todo list. See my answer. – trincot Commented Nov 11, 2016 at 9:41
  • You're not using includes, you're using indexOf? – Bergi Commented Nov 11, 2016 at 10:16
  • You can either use this.todos.map(t => t.toLowerCase()).includes(tasktitle) or this.todos.some(t => t.toLowerCase() == tasktitle) – Bergi Commented Nov 11, 2016 at 23:26
Add a ment  | 

2 Answers 2

Reset to default 1

I would suggest using a Map instead of an array for your todo list. A Map has the advantage that it provides key-based look-up in constant time, and does not store duplicate entries with the same key. You could then use the lower case variant as the key, and the original (mixed-case) string as the value for that key.

You could define todos as a Map instead of an array, and use the lower case string as the key:

constructor(props) {
    super(props);
    this.todos = new Map();
    this.addToDo('Get Up from bed'); 
    this.addToDo('Eat Breakfast');
}

Then, to add the task bees very straightforward, as a Map overwrites duplicates:

addToDo() {
    this.todos.set(task_title.toLowerCase(), task_title);
    this.setState({
        todos: this.todos
    });
 }

In rendering you would need to use Array.from(..., <map-function>), as .map is not defined for Map objects:

<div className="todos">
    {Array.from(this.todos, this.eachTodo)}
</div>

Which means the eachToDo method will receive a pair of strings (an array), instead of a string, and we want to display the second of the pair, so using index [1]:

eachTodo(task, i) {
    return (
        <Todo key={i} index={i}>{task[1]}</Todo>
    )
}

No, the only built-in methods are case sensitive. You'll need that loop and either toLowerCase or a case-insensitive regular expression (perhaps tucked away in a useful reusable function).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745258552a4619103.html

相关推荐

  • javascript - ES6 includes but case insensitive - Stack Overflow

    I know I can loop through array and just change value toLowerCase().I'm curious if there's a

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信