如何在JavaScript中过滤嵌套数组中的对象?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在JavaScript中过滤嵌套数组中的对象?相关的知识,希望对你有一定的参考价值。
如何通过JS中的useId
获得所有任务?我想在单击用户名时获取属于该用户的所有待办事项。如何通过JS中的useId
获取所有任务?我想在单击用户名时获取属于该用户的所有待办事项。如何通过JS中的useId
获取所有任务?我想在单击用户名时获取属于该用户的所有待办事项。
"users":[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret"
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette"
},
{"id": 3,
"name": "Clementine Bauch",
"username": "Samantha"
}
],
"todos":[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 3,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
},
{
"userId": 2,
"id": 3,
"title": "fugiat veniam minus",
"completed": false
},
{
"userId": 3,
"id": 4,
"title": "et porro tempora",
"completed": true
},
{
"userId": 2,
"id": 5,
"title": "laboriosam mollitia et enim quasi adipisci quia provident illum",
"completed": false
},
{
"userId": 1,
"id": 6,
"title": "qui ullam ratione quibusdam voluptatem quia omnis",
"completed": false
}]
import React, { useState, useEffect } from "react";
import Axios from "axios";
import { Link } from "react-router-dom";
function Users({ limit, match }) {
const [users, setUsers] = useState([]);
console.log(match);
useEffect(() => {
loadUsers();
}, []);
const loadUsers = async () => {
const result = await Axios.get(`http://localhost:3004/users`);
console.log(result);
setUsers(result.data);
};
return (
<div>
{users.slice(0, limit).map((user) => (
<Link to={`/users/${user.id}`}>
<h1>{user.title}</h1>
</Link>
))}
</div>
);
}
export default Users;
import React, { useState, useEffect } from "react";
function todos({ match }) {
const [todos, settodos] = useState([]);
console.log(match);
useEffect(() => {
loadtodos();
}, []);
const loadtodos = async () => {
const result = await fetch(
`http://localhost:3004/todos/${match.params.id}`
);
const todos = await result.json();
settodos(todos);
console.log(todos);
};
return <div>{todos.title}</div>;
}
export default todos;
答案
在这种情况下,ReactJ和香草javascript之间没有区别,您可以像这样简单地对todos
数组进行过滤
以上是关于如何在JavaScript中过滤嵌套数组中的对象?的主要内容,如果未能解决你的问题,请参考以下文章
带有 C#Driver 的 MongoDB:如何过滤嵌套对象数组中的字段