【问题标题】:Replace array in nested object in Javascript在Javascript中替换嵌套对象中的数组
【发布时间】:2021-12-20 05:53:15
【问题描述】:

我花了一天的大部分时间尝试替换现有嵌套对象的数组,但我不知道该怎么做。这是我原来的对象:

{
  "id": "a8df1653-238a-4f23-fe42-345c5d928b34",
  "webSections": {
    "id": "x58654a9-283b-4fa6-8466-3f7534783f8",
    "sections": [
      {
        "id": "92d7e428-4a5b-4f7e-bc7d-b761ca018922",
        "title": "Websites",
        "questions": [
          { 
            id: 'dee6e3a6-f207-f3db-921e-32a0b745557', 
            text: 'Website questions', 
            items: Array(11)
         }
        ]
      },
      {
        "id": "79e42d88-7dd0-4f70-b6b4-dea4b4a64ef3",
        "title": "Blogs",
        "questions": [
          ...
        ]
      },
      {
        "id": "439ded88-d7ed0-de70-b6b4-dea4b4a64e840",
        "title": "App questions",
        "questions": [
          ...
        ]
      }
    ]
}

我想替换原始对象或其副本中的问题数组。

const newMenu = [
{id: '34bb96c7-1eda-4f10-8acf-e6486296f4dd', text: 'Website questions', items: Array(24)},
{id: '520c2d3f-6117-4f6a-904f-2477e3347472', text: 'Blog questions', item: Array(7)},
{id: '302b658a-9d8c-4f53-80f6-3f2275bfble', title: 'App questions', items: Array(14)}
]

我试图通过它的索引来做到这一点,但不幸的是它不起作用。

 webSections.sections.forEach((item, index) => {
   return webSections.sections[index].questions, newMenu[index]);
 }

有人知道我做错了什么吗?

【问题讨论】:

    标签: javascript nested-object


    【解决方案1】:

    传递给forEach的回调返回的值不会在任何地方使用。

    如果你想避免改变原始对象和更新问题,你可以使用Array.prototype.map和对象传播语法。

    const object = {
      "id": "a8df1653-238a-4f23-fe42-345c5d928b34",
      "webSections": {
        "id": "x58654a9-283b-4fa6-8466-3f7534783f8",
        "sections": [
          {
            "id": "92d7e428-4a5b-4f7e-bc7d-b761ca018922",
            "title": "Websites",
            "questions": [
              { 
                id: 'dee6e3a6-f207-f3db-921e-32a0b745557', 
                ...
    
    const updatedObject = {
     ...object,
     webSections: {
      ...object.webSections,
      sections: object.webSections.sections.map((section, index) => ({...section, questions: newMenu[index]}))
     }
    }
    

    如果你只是想改变原始对象

    object.webSections.sections.forEach((_, index) => {
     section.questions = newMenu[index]
    })
    

    【讨论】:

      【解决方案2】:
          const newSections = myObj.webSections.sections.map((obj, index) => {
              const newQuestions = newItems[index];
              return {
                  ...obj,
                  questions: [newQuestions],
              };
          });
      
          console.log(newSections);
      

      MyObj 是主要对象。 这将产生新的sections数组,你可以将它与我想的主要对象结合起来......

      @Ramesh Reddy 给出了最彻底的答案。

      【讨论】:

        【解决方案3】:

        如果您不关心突变,最简单的方法是:

        myObject.webSections.sections.forEach((section, index) => {
          section.questions = newMenu[index].items;
        })
        

        您以错误的语法使用了“forEach”。查看MDN 的使用方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-23
          • 2020-04-07
          • 2018-01-24
          • 2016-04-13
          • 1970-01-01
          • 2022-11-17
          • 2020-10-19
          • 2019-10-12
          相关资源
          最近更新 更多