0

I have this pagination app. And it is working successfully. You can see the app working in this link: https://codepen.io/PiotrBerebecki/pen/pEYPbY

class TodoApp extends React.Component { constructor() { super(); this.state = { todos: ['a','b','c','d','e','f','g','h','i','j','k'], currentPage: 1, todosPerPage: 3 }; this.handleClick = this.handleClick.bind(this); } handleClick(event) { this.setState({ currentPage: Number(event.target.id) }); } render() { const { todos, currentPage, todosPerPage } = this.state; // Logic for displaying current todos const indexOfLastTodo = currentPage * todosPerPage; const indexOfFirstTodo = indexOfLastTodo - todosPerPage; const currentTodos = todos.slice(indexOfFirstTodo, indexOfLastTodo); const renderTodos = currentTodos.map((todo, index) => { return <li key={index}>{todo}</li>; }); // Logic for displaying page numbers const pageNumbers = []; for (let i = 1; i <= Math.ceil(todos.length / todosPerPage); i++) { pageNumbers.push(i); } const renderPageNumbers = pageNumbers.map(number => { return ( <li key={number} id={number} onClick={this.handleClick} > {number} </li> ); }); return ( <div> <ul> {renderTodos} </ul> <ul id="page-numbers"> {renderPageNumbers} </ul> </div> ); } } ReactDOM.render( <TodoApp />, document.getElementById('app') ); 

So I would like to change the initial array to a associative array, by putting a new key 'elements' like you can see below:

 "todos": { "elements": ['a','b','c','d','e','f','g','h','i','j','k'] }, 

How can I catch these datas after this change? I have tried this way:

const currentTodos = todos.map((t, index)=>{ t.elements.map((e,index)=>{ e.slice(indexOfFirstTodo, indexOfLastTodo); }) }) 

And it didn't work

1
  • 2
    Did you try todos.elements.map(...)? Commented Oct 29, 2018 at 17:27

2 Answers 2

1

slice copies a portion of an array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

You moved your array from todos to todos.elements. So you need to use slice on todos.elements:

const currentTodos = todos.elements.slice(indexOfFirstTodo, indexOfLastTodo); 
Sign up to request clarification or add additional context in comments.

2 Comments

I did this and it didn't work. There appeared an error message: Cannot read property 'slice' of undefined
Did you change todos in this.state in contructor as you planned?
0

With this model todos is an object not an array and map() is the javascript function for array.

"todos": { "elements": ['a','b','c','d','e','f','g','h','i','j','k'] }, 

This will help you implement pagination with javascript skip take methods with javascript arrays

[Edit] Answer from the link best suited for your case

var ary = [0,1,2,3,4,5,6,7]; alert(ary.splice(0,3).join(',')); 

1 Comment

It didn't work. Error message: Cannot read property 'splice' of undefined

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.