You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检查元素子项是否存在并将嵌套errors字段归集至顶层

如何归集JSON子对象的errors字段到顶层并移除子对象内的errors?

需求说明

我们有如下原始JSON数据:

{
  student: {
    firstName: 'Seif',
    lastName: 'Ali',
    countryOfCitizenship: 'EG',
    nationalId: '21106250100531',
    gender: 'MALE',
    dateOfBirth: '2011-06-25',
    errors: ['invalid nationalId '],
  },
  firstGuardian: {
    firstName: 'Ali',
    lastName: 'Ahmed',
    phoneNumber: '0101660666',
    email: 'jojo@gmail.com',
    countryOfCitizenship: 'EG',
    nationalId: '28108090100531',
    gender: 'MALE',
    dateOfBirth: '1981-08-09',
    errors: ['invalid phone Number '],
  },
  secondGuardian: {
    firstName: 'Aya',
    lastName: 'Adel',
    phoneNumber: '01016605555',
    email: 'aya@gmail.com',
    countryOfCitizenship: 'EG',
    nationalId: '29710250103561',
    gender: 'FEMALE',
    dateOfBirth: '1997-10-25',
  },
};

需要将其重构为以下格式:把每个子对象中的errors字段归集到顶层,同时移除子对象内的errors字段:

{
    student: {
        firstName: 'Seif',
        lastName: 'Ali',
        countryOfCitizenship: 'EG',
        nationalId: '21106250100531',
        gender: 'MALE',
        dateOfBirth: '2011-06-25',
    },
    firstGuardian: {
        firstName: 'Ali',
        lastName: 'Ahmed',
        phoneNumber: '0101660666',
        email: 'jojo@gmail.com',
        countryOfCitizenship: 'EG',
        nationalId: '28108090100531',
        gender: 'MALE',
        dateOfBirth: '1981-08-09',
    },
    secondGuardian: {
        firstName: 'Aya',
        lastName: 'Adel',
        phoneNumber: '01016605555',
        email: 'aya@gmail.com',
        countryOfCitizenship: 'EG',
        nationalId: '29710250103561',
        gender: 'FEMALE',
        dateOfBirth: '1997-10-25'
    },
    errors :  ['invalid nationalId ' ,'invalid phone Number ']
}

实现方法(以JavaScript为例)

可以通过遍历对象属性、检查errors字段存在性,完成归集和移除操作:

function restructureData(originalData) {
  // 复制原始数据并初始化顶层errors数组
  const restructured = { ...originalData, errors: [] };
  
  // 遍历所有顶层子对象
  Object.keys(restructured).forEach(key => {
    const item = restructured[key];
    // 检查当前子对象是否存在errors字段,且该字段是数组
    if (item && Array.isArray(item.errors)) {
      // 将子对象的errors合并到顶层errors数组
      restructured.errors.push(...item.errors);
      // 删除子对象中的errors字段
      delete item.errors;
    }
  });
  
  return restructured;
}

// 测试用原始数据
const originalData = {
  student: {
    firstName: 'Seif',
    lastName: 'Ali',
    countryOfCitizenship: 'EG',
    nationalId: '21106250100531',
    gender: 'MALE',
    dateOfBirth: '2011-06-25',
    errors: ['invalid nationalId '],
  },
  firstGuardian: {
    firstName: 'Ali',
    lastName: 'Ahmed',
    phoneNumber: '0101660666',
    email: 'jojo@gmail.com',
    countryOfCitizenship: 'EG',
    nationalId: '28108090100531',
    gender: 'MALE',
    dateOfBirth: '1981-08-09',
    errors: ['invalid phone Number '],
  },
  secondGuardian: {
    firstName: 'Aya',
    lastName: 'Adel',
    phoneNumber: '01016605555',
    email: 'aya@gmail.com',
    countryOfCitizenship: 'EG',
    nationalId: '29710250103561',
    gender: 'FEMALE',
    dateOfBirth: '1997-10-25',
  },
};

// 执行重构并输出结果
const result = restructureData(originalData);
console.log(result);

逻辑说明

  • 首先复制原始数据,避免修改原对象,同时初始化顶层的errors空数组。
  • 遍历顶层的每个属性,判断对应的子对象是否包含errors数组。
  • 如果存在errors数组,就将其中的元素展开添加到顶层errors中,然后删除子对象的errors字段。
  • 最后返回重构后的对象。

内容的提问来源于stack exchange,提问作者ahmed basher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:04:57