javascript - 關於循環js中的一段陣列問題
我想大声告诉你
我想大声告诉你 2017-05-19 10:47:10
0
1
527

js中原型是這樣的

cms.addNav([ { id: 1, pid: 0, node: '运行环境', url: '?s=Admin-Index-Right' }, { id: 2, pid: 0, node: '系统', url: '' }, { id: 3, pid: 2, node: '网络配置', url: '?s=Admin-Admin-Config' }, { id: 4, pid: 2, node: '导航管理', url: '?s=Admin-Nav-Show' }, { id: 6, pid: 2, node: '缓存管理', url: '?s=Admin-Cache-Show' }, { id: 7, pid: 2, node: '标签管理', url: '?s=Admin-Tag-Show' }, { id: 8, pid: 2, node: '模板管理', url: '?s=Admin-Tpl-Show' }, ], 0, 'id', 'pid', 'node', 'url');

下面這些我想透過ajax來取得

{ id: 1, pid: 0, node: '运行环境', url: '?s=Admin-Index-Right' }, { id: 2, pid: 0, node: '系统', url: '' }, { id: 3, pid: 2, node: '网络配置', url: '?s=Admin-Admin-Config' }, { id: 4, pid: 2, node: '导航管理', url: '?s=Admin-Nav-Show' }, { id: 6, pid: 2, node: '缓存管理', url: '?s=Admin-Cache-Show' }, { id: 7, pid: 2, node: '标签管理', url: '?s=Admin-Tag-Show' }, { id: 8, pid: 2, node: '模板管理', url: '?s=Admin-Tpl-Show' },

json資料輸出是這樣的

[ { "id": 1, "name": "sys", "title": "系统设置", "type": 1, "status": 1, "condition": "", "pid": 0, "level": 0, "sort": 7 }, { "id": 2, "name": "link", "title": "友情链接", "type": 1, "status": 1, "condition": "", "pid": 0, "level": 0, "sort": 4 }, { "id": 3, "name": "link/lst", "title": "链接列表", "type": 1, "status": 1, "condition": "", "pid": 2, "level": 1, "sort": 5 }, { "id": 4, "name": "link/del", "title": "删除链接", "type": 1, "status": 1, "condition": "", "pid": 3, "level": 2, "sort": 6 }, { "id": 11, "name": "conf/lst", "title": "配置列表", "type": 1, "status": 1, "condition": "", "pid": 1, "level": 1, "sort": 50 }, { "id": 10, "name": "link/add", "title": "添加链接", "type": 1, "status": 1, "condition": "", "pid": 3, "level": 2, "sort": 50 }, { "id": 9, "name": "conf/conf", "title": "配置项", "type": 1, "status": 1, "condition": "", "pid": 1, "level": 1, "sort": 50 }, { "id": 12, "name": "conf/add", "title": "添加配置", "type": 1, "status": 1, "condition": "", "pid": 11, "level": 2, "sort": 50 }, { "id": 13, "name": "conf/del", "title": "配置删除", "type": 1, "status": 1, "condition": "", "pid": 11, "level": 2, "sort": 50 }, { "id": 14, "name": "conf/edit", "title": "配置编辑", "type": 1, "status": 1, "condition": "", "pid": 11, "level": 2, "sort": 50 }, { "id": 15, "name": "admin", "title": "管理员", "type": 1, "status": 1, "condition": "", "pid": 0, "level": 0, "sort": 50 }, { "id": 16, "name": "admin/lst", "title": "管理员列表", "type": 1, "status": 1, "condition": "", "pid": 15, "level": 1, "sort": 50 }, { "id": 17, "name": "admin/add", "title": "管理员添加", "type": 1, "status": 1, "condition": "", "pid": 16, "level": 2, "sort": 50 }, { "id": 18, "name": "admin/del", "title": "管理员删除", "type": 1, "status": 1, "condition": "", "pid": 16, "level": 2, "sort": 50 }, { "id": 19, "name": "admin/edit", "title": "管理员修改", "type": 1, "status": 1, "condition": "", "pid": 16, "level": 2, "sort": 50 }, { "id": 20, "name": "link/edit", "title": "修改链接", "type": 1, "status": 1, "condition": "", "pid": 3, "level": 2, "sort": 50 }, { "id": 21, "name": "Video", "title": "影片", "type": 1, "status": 1, "condition": "", "pid": 0, "level": 0, "sort": 50 }, { "id": 22, "name": "video/show", "title": "影片列表", "type": 1, "status": 1, "condition": "", "pid": 21, "level": 1, "sort": 50 } ]

該怎麼寫才對呢?

我想大声告诉你
我想大声告诉你

全部回覆 (1)
某草草

用map做資料轉換

let newArr = arr.map(({id, pid, title, name}) => { return {id, pid, node: title, url: name}; })
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!