【问题标题】:find key and replace value with it in javascript / node js在javascript / node js中找到键并用它替换值
【发布时间】:2022-03-12 01:51:50
【问题描述】:

let data = [{
    system: {
      id: "4gSSbjCFEorYXqrgDIP2FA",
      type: "Entry",
      name: "User"
    },
    DataDetails: {
      shortOption: {
        "en-us": "some value"
      },
      mediaFile: [{
          sys: {
            type: "Link",
            link: "Entry",
            id: "7kRzyt4PFo",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Entry",
            id: "2OspeCtNK0s",
          },
        },
      ],
      mediaGalary: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "gHcw3Z1Ko",
          },
        },
        {
          sys: {
            type: "Link",
            linkType: "Asset",
            id: "h2cPiuU9jIz",
          },
        },
      ],
        mediaExplore: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "euwcbds3282",
          },
        },
        {
          sys: {
            type: "Link",
            linkType: "Asset",
            id: "jndsiw23912",
          },
        },
      ],
      singleMediaImage: {
        sys: {
          type: "Link",
          linkType: "Asset",
          id: "7kRzyt4PFo",
        },
      },
    },
  },
  {
    system: {
      id: "1aBOO8tu3lUsjtICuIbUM5",
      type: "Entry",
      name: "User"
    },
    DataDetails: {
      short: {
        "en-us": "details of shorts"
      },
      shortSlugOption: {
        "hi-In": "options"
      },
      booleanField: {
        kl: "true"
      },
    },
  },
  {
    system: {
      id: "2pOUGnI1oRD7nsrYs600HA",
      type: "Entry",
      name: "Dummy"
    },
    DataDetails: {
      testingNewValue: [{
          sys: {
            type: "Link",
            link: "Entry",
            id: "66rzYr2BpWL",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Entry",
            id: "1VTBHdLTdSW",
          },
        },
      ],
    },
  },
  {
    system: {
      id: "66rzYr2BpWL1VTBHdLTdSW",
      type: "Entry",
      name: "new"
    },
    DataDetails: {
      oneReference: {
        sys: {
          type: "Link",
          linkType: "Asset",
          id: "h2cPiuU9jIz",
        },
      },
      multiReference: [{
          sys: {
            type: "Link",
            link: "Asset",
            id: "gHcw3Z1Ko",
          },
        },
        {
          sys: {
            type: "Link",
            link: "Asset",
            id: "h2cPiuU9jIz",
          },
        },
      ],
    },
  },
  {
    system: {
      id: "cIb5mqEBRWDD6hrNmFmFE",
      type: "Entry",
      name: "new"
    },
    DataDetails: {
      testingNewValue: {
        "hi-IN": "jksdsdo"
      }
    },
  },
  {
    system: {
      id: "7kRzyt4PFrX13gHcw3Z1Ko",
      type: "Entry",
      name: "Dummy"
    },
    DataDetails: {
      testingNewValue: {
        "en-us": "kknksdo"
      }
    },
  },
];

let anotherObj = {
  "gHcw3Z1Ko": {
    status: true,
    tag: [],
    filename: "exute-image.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "h2cPiuU9jIz": {
    status: true,
    tag: [],
    filename: "wallpapers-6.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "7kRzyt4PFo": {
    status: true,
    tag: [],
    filename: "in-space-rk.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "euwcbds3282": {
    status: true,
    tag: [],
    filename: "justice-league.jpg",
    is_dir: false,
    parent_uid: null,
  },
  "jndsiw23912": {
    status: true,
    tag: [],
    filename: "batman.jpg",
    is_dir: false,
    parent_uid: null,
  },
};

let res = data.reduce((acc, curr) => {
  if (!acc[curr.system.name]) {
    acc[curr.system.name] = {};
  }

  let detailsObj = {};
  let assetArray=[];
  for (let key of Object.keys(curr.DataDetails)) {
    detailsObj[key] = Object.values(curr.DataDetails[key])[0];
    if (typeof Object.values(curr.DataDetails[key])[0] === "object") {
      Object.values(curr.DataDetails[key]).map((type) => {
        if (Array.isArray(type)) {
          type.map((link) => {
            if (link.sys.link === "Asset") {
              for (let id in anotherObj) {
                if (link.sys.id === id) {
                assetArray.push(anotherObj[id]);
                  detailsObj[key] = assetArray;
                }
              }
            }                
          });
        } else {
          if (type.sys !== undefined) {         
            // for single Asset
            if (type.sys.link === "Asset") {            
              for (let id in anotherObj) {
                if (type.sys.id === id) {  
                 detailsObj[key] = [anotherObj[id]];
                }
              }
            }
          }
        }
      });
    }
  }

  acc[curr.system.name][curr.system.id] = {
    title: curr.system.id,
    uid: curr.system.id,
    url: `/${curr.system.name}/${curr.system.id}`,
    ...detailsObj,
  };
  return acc;
}, {});

console.log(res);

我试图将两个对象数据放在一个输出中,但通过读取资产链接

但在多个数组列表中,输出显示为单个资产输出,因为 mediaFile 包含两个条目,但我不知道我在哪里做错了,它显示给我一个值

因为我的主要目的是用 anotherObj 关键数据替换 sys:{id:erfrdvsdf}

所以它应该是这样的,例如我的输出是这样的

"User": {
    "4gSSbjCFEorYXqrgDIP2FA": {
      "title": "4gSSbjCFEorYXqrgDIP2FA",
      "uid": "4gSSbjCFEorYXqrgDIP2FA",
      "url": "/User/4gSSbjCFEorYXqrgDIP2FA",
      "shortOption": "some value",
      "mediaFile": {
        "sys": {
          "type": "Link",
          "link": "Entry",
          "id": "7kRzyt4PFo"
        }
      },
      "mediaGalary": [
        {
          /**id:7**/
          "status": true,
          "tag": [],
          "filename": "in-space-rk.jpg",
          "is_dir": false,
          "parent_uid": null
        }
      ],
      "singleMediaImage": {
        "type": "Link",
        "linkType": "Asset",
        "id": "h2cPiuU9jIz"
      }
    },
  },

但应该是这样的

"User": {
    "4gSSbjCFEorYXqrgDIP2FA": {
      "title": "4gSSbjCFEorYXqrgDIP2FA",
      "uid": "4gSSbjCFEorYXqrgDIP2FA",
      "url": "/User/4gSSbjCFEorYXqrgDIP2FA",
      "shortOption": "some value",
      "mediaFile": {
        "sys": {
          "type": "Link",
          "link": "Entry",
          "id": "7kRzyt4PFo"
        }
      },
      "mediaGalary": [
        {
          "status": true,
          "tag": [],
          "filename": "in-space-rk.jpg",
          "is_dir": false,
          "parent_uid": null
        },
        {
          "status": true,
          "tag": [],
          "filename": "wallpapers-6.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ],
      "mediaExplore": [
        {
          "status": true,
          "tag": [],
          "filename": "justice-league",
          "is_dir": false,
          "parent_uid": null
        },
        {
          "status": true,
          "tag": [],
          "filename": "batman.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ],
      "singleMediaImage": [
        {
          "status": true,
          "tag": [],
          "filename": "wallpapers-6.jpg",
          "is_dir": false,
          "parent_uid": null
        },
      ]
    },
  },

但我的预期输出是我所期待的

因此,我被输入字段卡住了,因为我在输入字段中遇到了同样的问题,因为我编写了相同的代码来获取数组中的输入字段和单个输入字段

我不知道我的代码在哪里中断

我知道这是很长的代码,但请帮我解决这个错误,因为我没有继续前进

【问题讨论】:

    标签: javascript node.js arrays if-statement nodes


    【解决方案1】:

    如果你手动遍历复杂的对象,事情会变得非常复杂。维护代码将是一场噩梦。有很好的开源库可用于使用简单查询遍历复杂对象。例如,JsonPath
    在下面的演示中,我使用了 JsonPath。它处理AssetEntry 没有任何特殊处理。对于演示,我提供了所有 ID 的替换。

    let data = getData();
    let anotherObj = getAnotherObj();
    
    let res = data.reduce((acc, curr) => {
      if (!acc[curr.system.name]) {
        acc[curr.system.name] = {};
      }
    
      let detailsObj = {};
      Object.keys(curr.DataDetails).forEach(key => {
        let values = curr.DataDetails[key];
    
        // special handling to flatten single attribute objects
        if (values.length === undefined || values.length === 0) {
          let keys = Object.keys(values);
          if (keys.length == 1 && typeof values[keys[0]] !== 'object') {
            detailsObj[key] = values[keys[0]];
            return;
          }
        }
    
        // clone values to output
        detailsObj[key] = !Array.isArray(values) ? Object.assign({}, values) : [...values];
    
        // find and replace ids
        let ids = jsonpath.query(detailsObj[key], '$..id');
        ids.forEach((id, i) => {
          if (id in anotherObj) {
            if (Array.isArray(detailsObj[key]))
              detailsObj[key].splice(i, 1, anotherObj[id]);
            else
              detailsObj[key] = anotherObj[id];
          }
        });
      });
    
      acc[curr.system.name][curr.system.id] = {
        title: curr.system.id,
        uid: curr.system.id,
        url: `/${curr.system.name}/${curr.system.id}`,
        ...detailsObj,
      };
      return acc;
    }, {});
    
    document.body.insertAdjacentHTML('beforeend', `<pre>${JSON.stringify(res, undefined, 1)}</pre>`);
    console.log(res);
    
    
    
    function getData() {
      return [{
          system: {
            id: "4gSSbjCFEorYXqrgDIP2FA",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            shortOption: {
              "en-us": "some value"
            },
            mediaFile: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "7kRzyt4PFo",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "2OspeCtNK0s",
                },
              },
            ],
            mediaGalary: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  linkType: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
            singleMediaImage: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
          },
        },
        {
          system: {
            id: "1aBOO8tu3lUsjtICuIbUM5",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            short: {
              "en-us": "details of shorts"
            },
            shortSlugOption: {
              "hi-In": "options"
            },
            booleanField: {
              kl: "true"
            },
          },
        },
        {
          system: {
            id: "2pOUGnI1oRD7nsrYs600HA",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "66rzYr2BpWL",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "1VTBHdLTdSW",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "66rzYr2BpWL1VTBHdLTdSW",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            oneReference: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
            multiReference: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "cIb5mqEBRWDD6hrNmFmFE",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            testingNewValue: {
              "hi-IN": "jksdsdo"
            }
          },
        },
        {
          system: {
            id: "7kRzyt4PFrX13gHcw3Z1Ko",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: {
              "en-us": "kknksdo"
            }
          },
        }
      ];
    }
    
    
    function getAnotherObj() {
      return {
        "h2cPiuU9jIz": {
          status: true,
          tag: [],
          filename: "wallpapers-6.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "gHcw3Z1Ko": {
          status: true,
          tag: [],
          filename: "in-space-rk.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "7kRzyt4PFo": {
          status: true,
          tag: [],
          filename: "mediaFile0.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "2OspeCtNK0s": {
          status: true,
          tag: [],
          filename: "mediaFile1.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "66rzYr2BpWL": {
          type: 'entry',
          tag: [],
          entry_details: "this is first entry ***",
          is_secret: false,
        },
        "1VTBHdLTdSW": {
          type: 'entry',
          tag: [],
          entry_details: "some other entry ***",
          is_secret: true,
        },
      };
    }
    &lt;script src="https://cdn.jsdelivr.net/npm/jsonpath@1.1.1/jsonpath.min.js"&gt;&lt;/script&gt;

    注意,我还没有处理案例"shortOption": "some value"。无论如何,展平单值对象并不是这里的重点。



    或者你可以用单个语句替换原始对象中的所有资产和条目!

    let data = getData();
    let anotherObj = getAnotherObj();
    
    jsonpath.apply(data, '$[*].DataDetails..[?(@.sys.id)]', e => anotherObj[e.sys.id]);
    
    document.body.insertAdjacentHTML('beforeend', `<pre>${JSON.stringify(data, undefined, 1)}</pre>`);
    console.log(data);
    
    
    
    function getData() {
      return [{
          system: {
            id: "4gSSbjCFEorYXqrgDIP2FA",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            shortOption: {
              "en-us": "some value"
            },
            mediaFile: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "7kRzyt4PFo",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "2OspeCtNK0s",
                },
              },
            ],
            mediaGalary: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  linkType: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
            singleMediaImage: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
          },
        },
        {
          system: {
            id: "1aBOO8tu3lUsjtICuIbUM5",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            short: {
              "en-us": "details of shorts"
            },
            shortSlugOption: {
              "hi-In": "options"
            },
            booleanField: {
              kl: "true"
            },
          },
        },
        {
          system: {
            id: "2pOUGnI1oRD7nsrYs600HA",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "66rzYr2BpWL",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "1VTBHdLTdSW",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "66rzYr2BpWL1VTBHdLTdSW",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            oneReference: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
            multiReference: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "cIb5mqEBRWDD6hrNmFmFE",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            testingNewValue: {
              "hi-IN": "jksdsdo"
            }
          },
        },
        {
          system: {
            id: "7kRzyt4PFrX13gHcw3Z1Ko",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: {
              "en-us": "kknksdo"
            }
          },
        }
      ];
    }
    
    
    function getAnotherObj() {
      return {
        "h2cPiuU9jIz": {
          status: true,
          tag: [],
          filename: "wallpapers-6.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "gHcw3Z1Ko": {
          status: true,
          tag: [],
          filename: "in-space-rk.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "7kRzyt4PFo": {
          status: true,
          tag: [],
          filename: "mediaFile0.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "2OspeCtNK0s": {
          status: true,
          tag: [],
          filename: "mediaFile1.jpg",
          is_dir: false,
          parent_uid: null,
        },
        "66rzYr2BpWL": {
          type: 'entry',
          tag: [],
          entry_details: "this is first entry ****",
          is_secret: false,
        },
        "1VTBHdLTdSW": {
          type: 'entry',
          tag: [],
          entry_details: "some other entry ****",
          is_secret: true,
        },
      };
    }
    &lt;script src="https://cdn.jsdelivr.net/npm/jsonpath@1.1.1/jsonpath.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 嗨@Onkar 我只是试图通过传递值let values = Object.values(curr.DataDetails[key])[0]; 来获得"shortOption": "some value",但这并没有帮助我,因为我不想要那些en-ushi-In kl n 任何机会,因为我使用了你的这里的第一种方法
    • 我已经更新了答案中的第一个代码 sn-p。它展平单个属性对象。
    • 嗨@the hutt 我已经更新了我的问题你能帮我吗?
    • @Aakash 恢复原始问题并单独提出一个问题。我们可以使用this 之类的代码删除节点。将 `parent[key] = val;` 替换为 delete parent[key]
    【解决方案2】:

    您缺少一些值,因为您没有检查 linkType。 您的循环正在检查sys.link,但没有检查sys.linkType

    用途:

    link.sys.link === 'Asset' || link.sys.linkType === 'Asset'
    

    type.sys.link === 'Asset' || type.sys.linkType === 'Asset'
    

    此行还将覆盖您的数组:detailsObj[key] = [anotherObj[id]];

    替换为:

    assetArray.push(anotherObj[id]);
    detailsObj[key] = assetArray;
    

    要清楚,这段代码不好。一些建议:

    • 将代码拆分为更小的逻辑单元。不要嵌套 5 级循环。创建执行小操作的函数并相应地命名它们(例如,将逻辑放在名为 findAssetData 的函数中,而不是 for (let id in anotherObj) { ... }
    • 了解Array.prototype.forEachArray.prototype.map的区别(这段代码中的map使用是错误的)
    • 使用 eslint 等代码质量工具,如果您是初学者,它会有所帮助

    【讨论】:

      【解决方案3】:

      您的代码中有一个错误:anotherObj 有重复的键。如果您将其注销,您将看到该对象中只有两个条目:

      let anotherObj = {
        gHcw3Z1Ko: {
          status: true,
          tag: [],
          filename: "exute-image.jpg",
          is_dir: false,
          parent_uid: null,
        },
        h2cPiuU9jIz: {
          status: true,
          tag: [],
          filename: "wallpapers-6.jpg",
          is_dir: false,
          parent_uid: null,
        },
        gHcw3Z1Ko: {
          status: true,
          tag: [],
          filename: "in-space-rk.jpg",
          is_dir: false,
          parent_uid: null,
        },
      };
      
      console.log(anotherObj)

      另外,mediaFile 将条目数组压缩为一个 - 你没有描述你想用它做什么,所以我假设数组中的第一项就足够了。

      但这可能不是你想要的(在下面的 sn-p 中,我删除了重复键的第一个条目):

      let data = [{
          system: {
            id: "4gSSbjCFEorYXqrgDIP2FA",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            shortOption: {
              "en-us": "some value"
            },
            mediaFile: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "7kRzyt4PFo",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "2OspeCtNK0s",
                },
              },
            ],
            mediaGalary: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  linkType: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
            singleMediaImage: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
          },
        },
        {
          system: {
            id: "1aBOO8tu3lUsjtICuIbUM5",
            type: "Entry",
            name: "User"
          },
          DataDetails: {
            short: {
              "en-us": "details of shorts"
            },
            shortSlugOption: {
              "hi-In": "options"
            },
            booleanField: {
              kl: "true"
            },
          },
        },
        {
          system: {
            id: "2pOUGnI1oRD7nsrYs600HA",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: [{
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "66rzYr2BpWL",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Entry",
                  id: "1VTBHdLTdSW",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "66rzYr2BpWL1VTBHdLTdSW",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            oneReference: {
              sys: {
                type: "Link",
                linkType: "Asset",
                id: "h2cPiuU9jIz",
              },
            },
            multiReference: [{
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "gHcw3Z1Ko",
                },
              },
              {
                sys: {
                  type: "Link",
                  link: "Asset",
                  id: "h2cPiuU9jIz",
                },
              },
            ],
          },
        },
        {
          system: {
            id: "cIb5mqEBRWDD6hrNmFmFE",
            type: "Entry",
            name: "new"
          },
          DataDetails: {
            testingNewValue: {
              "hi-IN": "jksdsdo"
            }
          },
        },
        {
          system: {
            id: "7kRzyt4PFrX13gHcw3Z1Ko",
            type: "Entry",
            name: "Dummy"
          },
          DataDetails: {
            testingNewValue: {
              "en-us": "kknksdo"
            }
          },
        },
      ];
      
      let anotherObj = {
        h2cPiuU9jIz: {
          status: true,
          tag: [],
          filename: "wallpapers-6.jpg",
          is_dir: false,
          parent_uid: null,
        },
        gHcw3Z1Ko: {
          status: true,
          tag: [],
          filename: "in-space-rk.jpg",
          is_dir: false,
          parent_uid: null,
        },
      };
      
      // this is the function that actually merges the source
      // object with anotherObj
      const mergeGallery = (src, other) => {
        return src.reduce((a, c) => {
          if (typeof a[c.sys.id] === "undefined") a[c.sys.id] = {}
          a[c.sys.id] = { ...other[c.sys.id] }
          return a
        }, {})
      }
      
      // just to display that the generation of conditional attributes
      // can be extracted to their own functions
      const getShortOption = (el) => {
        const shortOption = el.system.name === "User" && el.DataDetails && el.DataDetails.shortOption ? {
          shortOption: el.DataDetails.shortOption["en-us"]
        } : {}
        return shortOption
      }
      
      const getFormatted = (el, otherGallery) => {
        return {
          title: el.system.id,
          uid: el.system.id,
          url: `/${el.system.name}/${el.system.id}`,
          ...getShortOption(el),
          ...(el.DataDetails && el.DataDetails.mediaFile && {
            mediaFile: el.DataDetails.mediaFile[0]
          }),
          ...(el.DataDetails && el.DataDetails.mediaGalary && {
            mediaGalary: mergeGallery(el.DataDetails.mediaGalary, otherGallery)
          }),
          ...(el.DataDetails && el.DataDetails.singleMediaImage && {
            singleMediaImage: el.DataDetails.singleMediaImage.sys
          }),
        }
      }
      
      const categorizeItems = (data) => {
        return data.reduce((a, c) => {
          if (typeof a[c.system.name] === "undefined") a[c.system.name] = {}
          a[c.system.name][c.system.id] = getFormatted(c, anotherObj)
          return a
        }, {})
      }
      
      const ret = categorizeItems(data)
      
      console.log(ret)

      建议

      1. 条件句可以短得多:
      const getShortOption = (el) => {
        const shortOption =
          el.system.name === "User" && el.DataDetails && el.DataDetails.shortOption
            ? { shortOption: el.DataDetails.shortOption["en-us"], }
            : {};
        return shortOption;
      };
      
      // could be written as 
      const getShortOption = (el) => {
        const shortOption = el.system.name === "User" && el.DataDetails?.shortOption?.["en-us"]
          ? { shortOption: el.DataDetails.shortOption["en-us"] }
          : {}
        return shortOption
      }
      

      如果您使用?. 链接,所有条件都更短 - 不幸的是 SO sn-ps 不知道这种语法。

      1. 如果您在代码中使用英文术语,请将Galary 的拼写更正为Gallery。如果你不这样做,这将是一个很好的混乱来源。

      【讨论】:

      • 实际上 mediaGalary mediaFile 这个名称键是用户生成的,如果用户添加具有不同名称的新键,它们每次都会更改,所以硬编码在这里对我没有帮助,就像我在代码中无法做到这一点 @987654329 @ as name 改变了这就是我匹配和替换代码的原因
      猜你喜欢
      • 1970-01-01
      • 2013-11-06
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 2015-11-11
      • 2020-05-12
      相关资源
      最近更新 更多