重新排列JSON数据以向嵌套对象添加ID

时间:2018-09-29 16:17:44

标签: javascript json

我正在尝试重新排列传入的JSON对象以在React组件中使用。

我收到的JSON对象是jsonData,这就是我当前的代码:

const jsonData = {
  "Jonas": {
    "position": "CTO",
    "employees": [{
      "Sophie": {
        "position": "VP Engineering",
        "employees": [{
            "Nick": {
              "position": "Team Lead",
              "employees": [{
                  "Pete": {
                    "position": "Backend Engineer",
                    "employees": []
                  }
                },
                {
                  "Barbara": {
                    "position": "Fronted Engineer",
                    "employees": []
                  }
                }
              ]
            }
          },
          {
            "Melissa": {
              "position": "Product Manager",
              "employees": []
            }
          }
        ]
      }
    }]
  }
}

const userList = [jsonData]

const formatData = list =>
  list.map(item => {
    let name, position, employees
    for (let key in item) {
      name = key
      position = item[key].position
      employees = item[key].employees ? item[key].employees : []
    }

    return {
      name,
      position,
      employees: employees ? formatData(employees) : employees
    }
  })

console.log(formatData(userList))

我正在尝试向每个对象添加新的id并将jsonData转换为数组。我正在获取输出,但无法按如下方式添加id-

[
  {
    "id": 0,
    "name": "Jonas",
    "position": "CTO",
    "employees": [
      {
        "id": 1,
        "name": "Sophie",
        "position": "VP Engineering",
        "employees": [
          {
            "id": 2,
            "name": "Nick",
            "position": "Team Lead",
            "employees": [
              {
                "id": 3,
                "name": "Pete",
                "position": "Backend Engineer",
                "employees": []
              },
              {
                "id": 4,
                "name": "Barbara",
                "position": "Fronted Engineer",
                "employees": []
              }
            ]
          },
          {
            "id": 5,
            "name": "Melissa",
            "position": "Product Manager",
            "employees": []
          }
        ]
      }
    ]
  }
]

如何为输出的每个对象添加id

3 个答案:

答案 0 :(得分:1)

只需添加一个变量id并在循环内递增。

const jsonData = {
  "Jonas": {
    "position": "CTO",
    "employees": [{
      "Sophie": {
        "position": "VP Engineering",
        "employees": [{
            "Nick": {
              "position": "Team Lead",
              "employees": [{
                  "Pete": {
                    "position": "Backend Engineer",
                    "employees": []
                  }
                },
                {
                  "Barbara": {
                    "position": "Fronted Engineer",
                    "employees": []
                  }
                }
              ]
            }
          },
          {
            "Melissa": {
              "position": "Product Manager",
              "employees": []
            }
          }
        ]
      }
    }]
  }
}

const userList = [jsonData]
var id=-1;
const formatData = list =>
  list.map(item => {
    let name, position, employees
    for (let key in item) {
      name = key
      position = item[key].position
      employees = item[key].employees ? item[key].employees : []
    }
   id=id+1;
    return {
      id,
      name,
      position,
      employees: employees ? formatData(employees) : employees
    }
     
  })

console.log(formatData(userList))

答案 1 :(得分:1)

您可以将Object.assignplugins.apply("net.vivin.gradle-semantic-build-versioning") 的计数器一起使用。

id
function convert(object) {
    const fn = o => (name => Object.assign(
        { id: id++, name },
        o[name],
        { employees: o[name].employees.map(fn)
    }))(Object.keys(o)[0]);

    var id = 0;
    return fn(object);
}

var data = { Jonas: { position: "CTO", employees: [{ Sophie: { position: "VP Engineering", employees: [{ Nick: { position: "Team Lead", employees: [{ Pete: { position: "Backend Engineer", employees: [] } }, { Barbara: { position: "Fronted Engineer", employees: [] } }] } }, { Melissa: { position: "Product Manager", employees: [] } }] } }] } },
    result = convert(data);

console.log(result);

答案 2 :(得分:0)

我认为这是一种更好的方法,如果添加更多,可以解决更多的嵌套员工数组

const jsonData = {
  "Jonas": {
    "position": "CTO",
    "employees": [{
      "Sophie": {
        "position": "VP Engineering",
        "employees": [{
          "Nick": {
            "position": "Team Lead",
            "employees": [{
              "Pete": {
                "position": "Backend Engineer",
                "employees": []
              }
            },
            {
              "Barbara": {
                "position": "Fronted Engineer",
                "employees": []
              }
            }
            ]
          }
        },
        {
          "Melissa": {
            "position": "Product Manager",
            "employees": []
          }
        }
        ]
      }
    }]
  }
}

const userList = [jsonData]


const newFormat = list => {

  findChildAndAddId(list, 0, list.length)
  return list
}

const findChildAndAddId = (item, count, parentNumber) => {
  if (item && Array.isArray(item)) {
    item.map((value, index) => {
      findChildAndAddId(value, count + index, item.length)
    })
  } else if (item) {
    Object.keys(item).map(key => {
      let value = item[key]
      item.id = count
      item.name = key
      if (value.position) {
        item.position = value.position
      }
      if (value.employees) {
        item.employees = value.employees
      }
      delete item[key]
      if (item.employees) {
        findChildAndAddId(item.employees, count + parentNumber)
      }
    })
  }
}

console.log(newFormat(userList))