搜索

首页  >  问答  >  正文

javascript - 关于循环js中的一段数组问题

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
    }
]

应该怎么写才对呢?

我想大声告诉你我想大声告诉你2768 天前571

全部回复(1)我来回复

  • 某草草

    某草草2017-05-19 10:49:10

    用map做数据转换

    let newArr = arr.map(({id, pid, title, name}) => {
        return {id, pid, node: title, url: name}; 
    })

    回复
    0
  • 取消回复