javascript 从REST API响应创建特定JSON对象

l7wslrjt  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(129)

我有一个场景,我正在uoloading一个csv文件在一个数据表和按钮点击我发送所有的行到一个端点API,他们将被处理一个接一个使用异步函数。我发送下面的负载到一个休息api,在异步函数

var b = [

    {
        "LastName": "LastName02",
        "Email": "FN02.LN02@gmail.com",
        "MobilePhone": "9198842"
    },
    {
        "LastName": "LastName03",
        "Email": "FN03.LN03@gmail.com",
        "MobilePhone": "+91 9884299071"
    },
]

一旦异步函数运行,我得到如下响应
对于第一条记录,我得到哪一部分在下面

resp.detailErrors = [
    {
      "errorCode": 7002,
      "field": "communicationNumbers.number",
      "value": "98842",
      "message": "For country India, Mobile length must be exactly 10 numeric digits, cannot start with zero."
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN02.LN02@gmail.com",
      "message": "Invalid Primary Email Address"
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN02.LN02@gmail.com",
      "message": "Invalid Primary Email Address"
    }
  ]

而对于第二张唱片,我得到

[
    {
        "errorCode": 7002,
        "field": "primaryEmailId",
        "value": "FN03.LN03@gmail.com",
        "message": "Invalid Primary Email Address"
    },
    {
        "errorCode": 7002,
        "field": "primaryEmailId",
        "value": "FN03.LN03@gmail.com",
        "message": "Invalid Primary Email Address"
    }
]

由于数据表的keyField是电子邮件,所以我想显示错误行。要显示错误行,我需要以特定的方式存储错误详细信息,如下所示

let errors = { rows: {}}

     errors.rows['FN02.LN02@gmail.com'] = {
                title: 'error',
                messages: ['invalid email', 'invalid phone'],
                fieldNames: ['Email', 'MobilePhone']
            };

            errors.rows['FN03.LN03@gmail.com'] = {
                title: 'error',
                messages: ['invalid email'],
                fieldNames: ['Email']
            };

我正在尝试的代码如下,但它不工作

let map = [];
let errors = { rows: {} }
var key = '';

var resp = {"detailedErrors":[{"errorCode":7002,"field":"contactEntity.communicationNumbers.number","value":"98842","message":"For country India, Mobile length must be exactly 10 numeric digits, cannot start with zero."},{"errorCode":7002,"field":"primaryEmailId","value":"FN02.LN02@gmail.com","message":"Invalid Primary Email Address"},{"errorCode":7002,"field":"primaryEmailId","value":"FN02.LN02@gmail.com","message":"Invalid Primary Email Address"}],"correlationIdentifier":"df3df8e876bce7357fcc84ccd36a76f1"}

 resp.detailedErrors.forEach(res=>{
    key = res.Email 
    if(res.value.includes('@')) {
       map.push({
         key:res.value,
         val:res.message
       });
    } else {
      console.log(res)
      if(key == undefined) {
      
        map.push({
         key:key,
         val:res.message
       });
      }
    }
  }) 
  console.log(map)
2ledvvac

2ledvvac1#

key = res.Email将始终将key设置为undefined,因为Email在示例中的res对象上不存在。也许您想使用res.value
你可能也不需要Map中的空对象,而不是let map = [{}];可以做let map = [];

bgtovc5b

bgtovc5b2#

要以逐行方式生成错误详细信息,必须迭代原始数据的每一行,然后检查是否存在与该行关联的任何错误。***KeyField***可用于标识原始数据中的每一行,然后将其与***resp.detailErrors***数组中的错误进行匹配。
下面是应生成所需结果的代码示例:

let errors = { rows: {} };
const data = [
  {
    "LastName": "LastName02",
    "Email": "FN02.LN02@gmail.com",
    "MobilePhone": "9198842"
  },
  {
    "LastName": "LastName03",
    "Email": "FN03.LN03@gmail.com",
    "MobilePhone": "+91 9884299071"
  },
];

const resp = {
  "detailErrors": [
    {
      "errorCode": 7002,
      "field": "communicationNumbers.number",
      "value": "98842",
      "message": "For country India, Mobile length must be exactly 10 numeric digits, cannot start with zero."
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN02.LN02@gmail.com",
      "message": "Invalid Primary Email Address"
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN02.LN02@gmail.com",
      "message": "Invalid Primary Email Address"
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN03.LN03@gmail.com",
      "message": "Invalid Primary Email Address"
    },
    {
      "errorCode": 7002,
      "field": "primaryEmailId",
      "value": "FN03.LN03@gmail.com",
      "message": "Invalid Primary Email Address"
    }
  ]
};

// first create an object that maps each row's keyField value to the row object
const dataMap = {};
for (const row of data) {
  dataMap[row.Email] = row;
}

// and then iterate over each error and add it to the corresponding row in the errors object
for (const error of resp.detailErrors) {
  const key = error.value;
  const row = dataMap[key];
  if (row) {
    if (!errors.rows[key]) {
      errors.rows[key] = {
        title: 'error',
        messages: [],
        fieldNames: []
      };
    }
    errors.rows[key].messages.push(error.message);
    errors.rows[key].fieldNames.push(error.field);
  }
}

console.log(errors);

输出:

{
  "rows": {
    "FN02.LN02@gmail.com": {
      "title": "error",
      "messages": [
        "Invalid Primary Email Address",
        "Invalid Primary Email Address"
      ],
      "fieldNames": [
        "primaryEmailId",
        "primaryEmailId"
      ]
    },
    "FN03.LN03@gmail.com": {
      "title": "error",
      "messages": [
        "Invalid Primary Email Address",
        "Invalid Primary Email Address"
      ],
      "fieldNames": [
        "primaryEmailId",
        "primaryEmailId"
      ]
    }
  }
}

相关问题