【问题标题】:How can I get and update a nested object within a complex object with array of objects? [duplicate]如何获取和更新具有对象数组的复杂对象中的嵌套对象? [复制]
【发布时间】:2020-06-08 18:31:24
【问题描述】:

我手头有这个课程结构,但我不知道如何通过 ID 获取课程对象,以及如何使用修改后的属性值更新课程对象?

如果有人能照亮正确的道路,将不胜感激。

{
   id: 1,
   name: 'An Amazing Course',
   description: 'Mauris ac efficitur enim, nec commodo quam. Nunc vehicula blandit porta.',
   sections: [
      {
         id: 1,
         sort_order: 1,
         name: 'Section one',
         lessons: [
            {
               id: 1,
               sort_order: 1,
               name: 'Lesson One'
            },
            {
               id: 3,
               sort_order: 2,
               name: 'Lesson Three'
            },
            {
               id: 2,
               sort_order: 3,
               name: 'Lesson Two'
            }
         ]

      },
      {
         id: 2,
         sort_order: 2,
         name: 'Section Two',
         lessons: [
            {
               id: 4,
               sort_order: 1,
               name: 'Lesson Four'
            },
            {
               id: 5,
               sort_order: 2,
               name: 'Lesson Five'
            },
            {
               id: 6,
               sort_order: 3,
               name: 'Lesson Six'
            }
         ]
      }
   ]
}

【问题讨论】:

  • 整个对象的关键是什么?是 JSON 还是那些不应该存在的花括号?
  • @Daniel_Knights 整个对象包装在一个名为course 的变量中。 let course = { id: 1, name: 'An Amazing Course', ... }
  • 哦,好吧。例如,您可以这样做 course.sections[0].lessons[0].name = "New Lesson One"

标签: javascript ecmascript-6 javascript-objects


【解决方案1】:
const obj = {
   id: 1,
   name: 'An Amazing Course',
   description: 'Mauris ac efficitur enim, nec commodo quam. Nunc vehicula blandit porta.',
   sections: [
      {
         id: 1,
         sort_order: 1,
         name: 'Section one',
         lessons: [
            {
               id: 1,
               sort_order: 1,
               name: 'Lesson One'
            },
            {
               id: 3,
               sort_order: 2,
               name: 'Lesson Three'
            },
            {
               id: 2,
               sort_order: 3,
               name: 'Lesson Two'
            }
         ]

      },
      {
         id: 2,
         sort_order: 2,
         name: 'Section Two',
         lessons: [
            {
               id: 4,
               sort_order: 1,
               name: 'Lesson Four'
            },
            {
               id: 5,
               sort_order: 2,
               name: 'Lesson Five'
            },
            {
               id: 6,
               sort_order: 3,
               name: 'Lesson Six'
            }
         ]
      }
   ]
}

const lessons = [].concat.apply([], Object.values(obj.sections).map(section => section.lessons))

const id = //whichever

const selected = lessons.find(lesson => lesson.id === id)

您可以尝试使用类似的方法,将对象展平以将所有课程放在一个普通数组中。这样你就可以毫无问题地使用查找/过滤器了。

通过稍微复杂一点的逻辑,您可以在平面课程数组中维护课程的 ID 引用和课程

[].concat.apply([], Object.values(obj.sections).map(section => 
  section.lessons.map(lesson => ({
    ...lesson,
    courseId: obj.id,
    sectionId: section.id
  }))
))

【讨论】:

  • 非常感谢您的快速回复。当您展平数组时,它不会在更新课程对象的属性时失去对名为 obj 的原始对象的引用吗?
  • 按照我的做法,是的,您丢失了参考。但是你可以扩展内部映射的返回来添加这个引用
  • 我添加了一些更复杂的 sn-p 来维护父引用
  • 这正是我所追求的。谢谢@al-hill
【解决方案2】:

这是通过 id 搜索课程

function findless(ids){
           let  result
         const lesvalues=Object.values(les)

          lesvalues.filter(x=>Array.isArray(x)?x.forEach(y=>y.lessons.find(x=>x.id==ids?result=x.name:null)):null)
          return result
         }
         console.log(findless(3))

【讨论】:

    猜你喜欢
    • 2017-12-27
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    相关资源
    最近更新 更多