我正在开发一个网站的堆栈:React, redux, typescript。我不能输出一个嵌套数组的数据从JSON中的数据对象我的代码:
app.tsx
const App: React.FC = () => {
const {tasks, loading, error} = useTypedSelector(state => state.task)
const dispatch: Dispatch<any> = useDispatch()
useEffect(() => {
dispatch(fetchTasks())
}, [])
if (loading) {
return <h1>Идет загрузка...</h1>
}
if (error) {
return <h1>{error}</h1>
}
return (
<div className="Gant_Container">
<div>
<p className="Project_Period">{Object.values(tasks)[0]} / {Object.values(tasks)[1]}</p>
</div>
<div>
{Object.values(tasks).map((task, id) => {
return (<div key={id}>
{task.id}
{task.title}
{chart.start}
{chart.end}
</div>)
})}
</div>
</div>
);
};
export default Gantt_Container;
存储/index.ts
export const store = createStore(rootReducer, applyMiddleware(thunk))
异径接头/index.ts
export const rootReducer = combineReducers({
task: taskReducer,
})
export type RootState = ReturnType<typeof rootReducer>
异径接头/taskReducer.tsx
const initialState: TaskState = {
tasks: [],
loading: false,
error: null
}
export const taskReducer = (state = initialState, action: TaskAction): TaskState => {
switch (action.type) {
case TaskActionTypes.FETCH_TASKS:
return {loading: true, error: null, tasks: []}
case TaskActionTypes.FETCH_TASKS_SUCCESS:
return {loading: false, error: null, tasks: action.payload}
case TaskActionTypes.FETCH_TASKS_ERROR:
return {loading: false, error: action.payload, tasks: []}
default:
return state
}
}
action-creators/task.ts
export const fetchTasks = () => {
return async (dispatch: Dispatch<TaskAction>) => {
try {
dispatch({type: TaskActionTypes.FETCH_TASKS})
const response = await axios.get("") // The data is coming from the backend, I have hidden the data
dispatch({type: TaskActionTypes.FETCH_TASKS_SUCCESS, payload: response.data})
} catch (e) {
dispatch({
type: TaskActionTypes.FETCH_TASKS_ERROR,
payload: 'Произошла ошибка при загрузке данных'
})
}
}
}
类型/task.ts
export interface TaskState {
tasks: any[];
loading: boolean;
error: null | string;
}
export enum TaskActionTypes {
FETCH_TASKS = 'FETCH_TASKS',
FETCH_TASKS_SUCCESS = 'FETCH_TASKS_SUCCESS',
FETCH_TASKS_ERROR = 'FETCH_TASKS_ERROR'
}
interface FetchTasksAction {
type: TaskActionTypes.FETCH_TASKS;
}
interface FetchTasksSuccessAction {
type: TaskActionTypes.FETCH_TASKS_SUCCESS;
payload: any[]
}
interface FetchTasksErrorAction {
type: TaskActionTypes.FETCH_TASKS_ERROR;
payload: string;
}
export type TaskAction = FetchTasksAction | FetchTasksSuccessAction | FetchTasksErrorAction
useTypedSelector.ts
export const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector
. json
{
"name": "Project",
"data": "2022",
"task": {
"id": 1,
"title": "Apple",
"start": "2021",
"end": "2022",
"sub": [
{
"id": 2,
"title": "tomato",
"start": "2021",
"end": "2022",
"sub": [
{
"id": 3,
"title": "Orange",
"start": "2019",
"end": "2020",
"sub": [
{
"id": 4,
"title": "Banana",
"start": "2022",
"end": "2022",
"sub": [
{
"id": 5,
"title": "Strawberry",
"start": "2015",
"end": "2018"
},
{
"id": 6,
"title": "cherry",
"period_start": "2001,
"period_end": "2003"
}
]
}
]
}
]
}
]
}
}
不幸的是,我无法编辑这个json文件。
sub之前我可以输出所有数据,sub之后我不能输出。我需要输出json中的所有数据。
我从网上尝试了很多方法,但都没有成功
这是如何将嵌套对象转换为单个数组的解决方案,请在您的代码中这样使用:
它将像递归函数一样工作。
const obj = {
name: 'Project',
data: '2022',
task: {
id: 1,
title: 'Apple',
start: '2021',
end: '2022',
sub: [{
id: 2,
title: 'tomato',
start: '2021',
end: '2022',
sub: [{
id: 3,
title: 'Orange',
start: '2019',
end: '2020',
sub: [{
id: 4,
title: 'Banana',
start: '2022',
end: '2022',
sub: [{
id: 5,
title: 'Strawberry',
start: '2015',
end: '2018',
},
{
id: 6,
title: 'cherry',
start: '2001',
end: '2003',
},
],
}, ],
}, ],
}, ],
},
};
const arr = [];
const foo = (task) => {
if (!task.id) return;
arr.push({
id: task.id,
title: task.title,
start: task.start,
end: task.end,
});
if (task.sub && task.sub.length > 0) task.sub.forEach(item => foo(item));
};
foo(obj.task);
console.log('>>>>> arr : ', arr);
我想你错过的是object.keys的用户:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
它允许你返回一个可以映射的对象的键数组。
我的建议是这样做:
const GanttContainer: React.FC = () => {
const {tasks, loading, error} = useTypedSelector(state => state.task)
const dispatch: Dispatch<any> = useDispatch()
useEffect(() => {
dispatch(fetchTasks())
}, [])
if (loading) {
return <h1>Идет загрузка...</h1>
}
if (error) {
return <h1>{error}</h1>
}
return (
....
{Object.keys(tasks).map((taskKeys, id) => {
return (
<div key={id}>
{tasks[taskKeys]}
</div>)
....
);
};
export default GanttContainer;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>