Filter function returns an empty array?
So when I call my handleRemove function here, if i return and console.log el
, I get each one of my todos. However, if I return and console.log el[index], OR el[index].id, I get nothing but an empty array. I've looked into the docs of the Array.prototype.filter method but I don't understand what I am doing wrong here.
import React, { Component } from 'react';
import TodoInput from './todo-input';
import TodoList from './TodoList';
class App extends Component {
constructor(props){
super(props);
this.state = {
todos: ,
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemove = this.handleRemove.bind(this);
};
handleChange = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
title: this.state.inputValue,
id: Date.now(),
done: false
};
this.setState((prevState) => ({
todos: [...prevState.todos, newTodo],
inputValue: ''
}));
}
handleRemove = (e) => {
e.preventDefault();
const newTodos = this.state.todos.filter((el, index) => el[index]);
// this.setState((prevState) => ({
// todos: newTodos
// }));
console.log(newTodos);
}
render() {
return (
<div>
<TodoInput
value={this.state.inputValue}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
/>
<TodoList
todosArr={this.state.todos}
onRemove={this.handleRemove}
/>
</div>
);
}
}
export default App;
TodoList.js
import React, { Component } from 'react';
import TodoItem from './TodoItem';
class TodoList extends Component {
render() {
const mappedTodos = this.props.todosArr.map((todo, index) =>
<TodoItem
key={index}
todoTitle={todo.title}
handleRemove={this.props.onRemove}
todoId={todo.id}
/>
);
return (
mappedTodos
);
}
}
export default TodoList;
javascript arrays reactjs filter
|
show 1 more comment
So when I call my handleRemove function here, if i return and console.log el
, I get each one of my todos. However, if I return and console.log el[index], OR el[index].id, I get nothing but an empty array. I've looked into the docs of the Array.prototype.filter method but I don't understand what I am doing wrong here.
import React, { Component } from 'react';
import TodoInput from './todo-input';
import TodoList from './TodoList';
class App extends Component {
constructor(props){
super(props);
this.state = {
todos: ,
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemove = this.handleRemove.bind(this);
};
handleChange = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
title: this.state.inputValue,
id: Date.now(),
done: false
};
this.setState((prevState) => ({
todos: [...prevState.todos, newTodo],
inputValue: ''
}));
}
handleRemove = (e) => {
e.preventDefault();
const newTodos = this.state.todos.filter((el, index) => el[index]);
// this.setState((prevState) => ({
// todos: newTodos
// }));
console.log(newTodos);
}
render() {
return (
<div>
<TodoInput
value={this.state.inputValue}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
/>
<TodoList
todosArr={this.state.todos}
onRemove={this.handleRemove}
/>
</div>
);
}
}
export default App;
TodoList.js
import React, { Component } from 'react';
import TodoItem from './TodoItem';
class TodoList extends Component {
render() {
const mappedTodos = this.props.todosArr.map((todo, index) =>
<TodoItem
key={index}
todoTitle={todo.title}
handleRemove={this.props.onRemove}
todoId={todo.id}
/>
);
return (
mappedTodos
);
}
}
export default TodoList;
javascript arrays reactjs filter
1
Inthis.state.todos.filter((el, index) => el[index]);
,index
is the position ofel
itself insidethis.state.todos
. Accessingel[index]
doesn't make sense. You get an empty array because accessingel[index]
likely returnsundefined
, i.e. a falsy value..filter
removes all elements for which the callback returns a falsy value. And you should be able to see thatel[index]
doesn't make sense if you look at the properties thatel
actually has (title
,id
,done
). However, we cannot really help you if we don't know what you are trying to achieve here.
– Felix Kling
Nov 20 '18 at 21:51
1
What are you trying to accompish in thefilter
call? Looking at the code, it seems you may be confused about whatfilter
does / how it works.
– Tex
Nov 20 '18 at 21:51
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
el
is an element in the array.index
is the index of that element in the array.['a','b'].filter((el, index) => console.log(el, index))
will log'a' 0
followed by'b' 1
.
– Tex
Nov 20 '18 at 22:08
|
show 1 more comment
So when I call my handleRemove function here, if i return and console.log el
, I get each one of my todos. However, if I return and console.log el[index], OR el[index].id, I get nothing but an empty array. I've looked into the docs of the Array.prototype.filter method but I don't understand what I am doing wrong here.
import React, { Component } from 'react';
import TodoInput from './todo-input';
import TodoList from './TodoList';
class App extends Component {
constructor(props){
super(props);
this.state = {
todos: ,
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemove = this.handleRemove.bind(this);
};
handleChange = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
title: this.state.inputValue,
id: Date.now(),
done: false
};
this.setState((prevState) => ({
todos: [...prevState.todos, newTodo],
inputValue: ''
}));
}
handleRemove = (e) => {
e.preventDefault();
const newTodos = this.state.todos.filter((el, index) => el[index]);
// this.setState((prevState) => ({
// todos: newTodos
// }));
console.log(newTodos);
}
render() {
return (
<div>
<TodoInput
value={this.state.inputValue}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
/>
<TodoList
todosArr={this.state.todos}
onRemove={this.handleRemove}
/>
</div>
);
}
}
export default App;
TodoList.js
import React, { Component } from 'react';
import TodoItem from './TodoItem';
class TodoList extends Component {
render() {
const mappedTodos = this.props.todosArr.map((todo, index) =>
<TodoItem
key={index}
todoTitle={todo.title}
handleRemove={this.props.onRemove}
todoId={todo.id}
/>
);
return (
mappedTodos
);
}
}
export default TodoList;
javascript arrays reactjs filter
So when I call my handleRemove function here, if i return and console.log el
, I get each one of my todos. However, if I return and console.log el[index], OR el[index].id, I get nothing but an empty array. I've looked into the docs of the Array.prototype.filter method but I don't understand what I am doing wrong here.
import React, { Component } from 'react';
import TodoInput from './todo-input';
import TodoList from './TodoList';
class App extends Component {
constructor(props){
super(props);
this.state = {
todos: ,
inputValue: ''
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRemove = this.handleRemove.bind(this);
};
handleChange = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
});
}
handleSubmit = (e) => {
e.preventDefault();
const newTodo = {
title: this.state.inputValue,
id: Date.now(),
done: false
};
this.setState((prevState) => ({
todos: [...prevState.todos, newTodo],
inputValue: ''
}));
}
handleRemove = (e) => {
e.preventDefault();
const newTodos = this.state.todos.filter((el, index) => el[index]);
// this.setState((prevState) => ({
// todos: newTodos
// }));
console.log(newTodos);
}
render() {
return (
<div>
<TodoInput
value={this.state.inputValue}
onChange={this.handleChange}
onSubmit={this.handleSubmit}
/>
<TodoList
todosArr={this.state.todos}
onRemove={this.handleRemove}
/>
</div>
);
}
}
export default App;
TodoList.js
import React, { Component } from 'react';
import TodoItem from './TodoItem';
class TodoList extends Component {
render() {
const mappedTodos = this.props.todosArr.map((todo, index) =>
<TodoItem
key={index}
todoTitle={todo.title}
handleRemove={this.props.onRemove}
todoId={todo.id}
/>
);
return (
mappedTodos
);
}
}
export default TodoList;
javascript arrays reactjs filter
javascript arrays reactjs filter
edited Nov 21 '18 at 18:47
Cole Gonzales
asked Nov 20 '18 at 21:48
Cole GonzalesCole Gonzales
329
329
1
Inthis.state.todos.filter((el, index) => el[index]);
,index
is the position ofel
itself insidethis.state.todos
. Accessingel[index]
doesn't make sense. You get an empty array because accessingel[index]
likely returnsundefined
, i.e. a falsy value..filter
removes all elements for which the callback returns a falsy value. And you should be able to see thatel[index]
doesn't make sense if you look at the properties thatel
actually has (title
,id
,done
). However, we cannot really help you if we don't know what you are trying to achieve here.
– Felix Kling
Nov 20 '18 at 21:51
1
What are you trying to accompish in thefilter
call? Looking at the code, it seems you may be confused about whatfilter
does / how it works.
– Tex
Nov 20 '18 at 21:51
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
el
is an element in the array.index
is the index of that element in the array.['a','b'].filter((el, index) => console.log(el, index))
will log'a' 0
followed by'b' 1
.
– Tex
Nov 20 '18 at 22:08
|
show 1 more comment
1
Inthis.state.todos.filter((el, index) => el[index]);
,index
is the position ofel
itself insidethis.state.todos
. Accessingel[index]
doesn't make sense. You get an empty array because accessingel[index]
likely returnsundefined
, i.e. a falsy value..filter
removes all elements for which the callback returns a falsy value. And you should be able to see thatel[index]
doesn't make sense if you look at the properties thatel
actually has (title
,id
,done
). However, we cannot really help you if we don't know what you are trying to achieve here.
– Felix Kling
Nov 20 '18 at 21:51
1
What are you trying to accompish in thefilter
call? Looking at the code, it seems you may be confused about whatfilter
does / how it works.
– Tex
Nov 20 '18 at 21:51
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
el
is an element in the array.index
is the index of that element in the array.['a','b'].filter((el, index) => console.log(el, index))
will log'a' 0
followed by'b' 1
.
– Tex
Nov 20 '18 at 22:08
1
1
In
this.state.todos.filter((el, index) => el[index]);
, index
is the position of el
itself inside this.state.todos
. Accessing el[index]
doesn't make sense. You get an empty array because accessing el[index]
likely returns undefined
, i.e. a falsy value. .filter
removes all elements for which the callback returns a falsy value. And you should be able to see that el[index]
doesn't make sense if you look at the properties that el
actually has (title
, id
, done
). However, we cannot really help you if we don't know what you are trying to achieve here.– Felix Kling
Nov 20 '18 at 21:51
In
this.state.todos.filter((el, index) => el[index]);
, index
is the position of el
itself inside this.state.todos
. Accessing el[index]
doesn't make sense. You get an empty array because accessing el[index]
likely returns undefined
, i.e. a falsy value. .filter
removes all elements for which the callback returns a falsy value. And you should be able to see that el[index]
doesn't make sense if you look at the properties that el
actually has (title
, id
, done
). However, we cannot really help you if we don't know what you are trying to achieve here.– Felix Kling
Nov 20 '18 at 21:51
1
1
What are you trying to accompish in the
filter
call? Looking at the code, it seems you may be confused about what filter
does / how it works.– Tex
Nov 20 '18 at 21:51
What are you trying to accompish in the
filter
call? Looking at the code, it seems you may be confused about what filter
does / how it works.– Tex
Nov 20 '18 at 21:51
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
But when I return
el
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.– Cole Gonzales
Nov 20 '18 at 22:00
But when I return
el
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.– Cole Gonzales
Nov 20 '18 at 22:00
el
is an element in the array. index
is the index of that element in the array. ['a','b'].filter((el, index) => console.log(el, index))
will log 'a' 0
followed by 'b' 1
.– Tex
Nov 20 '18 at 22:08
el
is an element in the array. index
is the index of that element in the array. ['a','b'].filter((el, index) => console.log(el, index))
will log 'a' 0
followed by 'b' 1
.– Tex
Nov 20 '18 at 22:08
|
show 1 more comment
1 Answer
1
active
oldest
votes
Filter expects a true/false response, so you need to compare el
to some kind of condition, like for example an id.
Demo
https://repl.it/@AnonymousSB/SO53402083
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 123, value: 'Water the dog' },
{ id: 456, value: 'Rake the floor' }
]
}
}
handleRemove(e, id) {
const todos = this.state.todos.filter(todo => todo.id !== id)
this.setState({ todos })
}
render() {
return (
<section style={{width: 500}}>
<ul>
{this.state.todos.map(todo => {
return <li onClick={(e) => this.handleRemove(e, todo.id)}>{todo.value}</li>
})}
</ul>
</section>
);
}
}
export default App;
1
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
|
show 14 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402083%2ffilter-function-returns-an-empty-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Filter expects a true/false response, so you need to compare el
to some kind of condition, like for example an id.
Demo
https://repl.it/@AnonymousSB/SO53402083
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 123, value: 'Water the dog' },
{ id: 456, value: 'Rake the floor' }
]
}
}
handleRemove(e, id) {
const todos = this.state.todos.filter(todo => todo.id !== id)
this.setState({ todos })
}
render() {
return (
<section style={{width: 500}}>
<ul>
{this.state.todos.map(todo => {
return <li onClick={(e) => this.handleRemove(e, todo.id)}>{todo.value}</li>
})}
</ul>
</section>
);
}
}
export default App;
1
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
|
show 14 more comments
Filter expects a true/false response, so you need to compare el
to some kind of condition, like for example an id.
Demo
https://repl.it/@AnonymousSB/SO53402083
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 123, value: 'Water the dog' },
{ id: 456, value: 'Rake the floor' }
]
}
}
handleRemove(e, id) {
const todos = this.state.todos.filter(todo => todo.id !== id)
this.setState({ todos })
}
render() {
return (
<section style={{width: 500}}>
<ul>
{this.state.todos.map(todo => {
return <li onClick={(e) => this.handleRemove(e, todo.id)}>{todo.value}</li>
})}
</ul>
</section>
);
}
}
export default App;
1
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
|
show 14 more comments
Filter expects a true/false response, so you need to compare el
to some kind of condition, like for example an id.
Demo
https://repl.it/@AnonymousSB/SO53402083
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 123, value: 'Water the dog' },
{ id: 456, value: 'Rake the floor' }
]
}
}
handleRemove(e, id) {
const todos = this.state.todos.filter(todo => todo.id !== id)
this.setState({ todos })
}
render() {
return (
<section style={{width: 500}}>
<ul>
{this.state.todos.map(todo => {
return <li onClick={(e) => this.handleRemove(e, todo.id)}>{todo.value}</li>
})}
</ul>
</section>
);
}
}
export default App;
Filter expects a true/false response, so you need to compare el
to some kind of condition, like for example an id.
Demo
https://repl.it/@AnonymousSB/SO53402083
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ id: 123, value: 'Water the dog' },
{ id: 456, value: 'Rake the floor' }
]
}
}
handleRemove(e, id) {
const todos = this.state.todos.filter(todo => todo.id !== id)
this.setState({ todos })
}
render() {
return (
<section style={{width: 500}}>
<ul>
{this.state.todos.map(todo => {
return <li onClick={(e) => this.handleRemove(e, todo.id)}>{todo.value}</li>
})}
</ul>
</section>
);
}
}
export default App;
edited Nov 20 '18 at 22:10
answered Nov 20 '18 at 21:54
AnonymousSBAnonymousSB
2,229221
2,229221
1
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
|
show 14 more comments
1
But when I returnel
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.
– Cole Gonzales
Nov 20 '18 at 22:00
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
1
1
But when I return
el
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.– Cole Gonzales
Nov 20 '18 at 22:00
But when I return
el
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.– Cole Gonzales
Nov 20 '18 at 22:00
1
1
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
im wanting to save it to props and access it there. I want to read all todos from state, then remove an element by clicking on the button in the DOM, I want it to remove the todo item from state, thus removing it from the DOM PS: Sorry for the long respond times, I seriously appreciate your quickness
– Cole Gonzales
Nov 21 '18 at 0:49
1
1
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
Well now it's telling me my this.props.todosArr.map() is not a function. When it was clearly mapping fine before. As I understand it, maps only work on arrays. Which in my TodoList, todosArr points to the todos array in my state. This is the only solution that has triggered this error though, so could it be something related to that? This is odd...
– Cole Gonzales
Nov 21 '18 at 17:55
1
1
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
I have been messing with it and todo always returns the entire todos array, and id always points to an empty array, whether I call it by itself or call todo.id. Obviously it's cause our code is different, but I am still pretty new to this and I really want to understand it well. I don't understand why adding that index parameter doesn't allow me to read correctly. I'm going to add in my TodoList.js maybe that will shed some light.
– Cole Gonzales
Nov 21 '18 at 18:41
1
1
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
Yeah its an array of objects with {title:... id:... done:...}
– Cole Gonzales
Nov 22 '18 at 4:21
|
show 14 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402083%2ffilter-function-returns-an-empty-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
In
this.state.todos.filter((el, index) => el[index]);
,index
is the position ofel
itself insidethis.state.todos
. Accessingel[index]
doesn't make sense. You get an empty array because accessingel[index]
likely returnsundefined
, i.e. a falsy value..filter
removes all elements for which the callback returns a falsy value. And you should be able to see thatel[index]
doesn't make sense if you look at the properties thatel
actually has (title
,id
,done
). However, we cannot really help you if we don't know what you are trying to achieve here.– Felix Kling
Nov 20 '18 at 21:51
1
What are you trying to accompish in the
filter
call? Looking at the code, it seems you may be confused about whatfilter
does / how it works.– Tex
Nov 20 '18 at 21:51
I am trying to use filter to return all of the todos which id's do not match the clicked todos id.
– Cole Gonzales
Nov 20 '18 at 21:59
But when I return
el
, I get back the todos array. So how does accessing that index not access the elements in the array? That must be where im confused.– Cole Gonzales
Nov 20 '18 at 22:00
el
is an element in the array.index
is the index of that element in the array.['a','b'].filter((el, index) => console.log(el, index))
will log'a' 0
followed by'b' 1
.– Tex
Nov 20 '18 at 22:08