【问题标题】:Splice an array of objects multiple times多次拼接对象数组
【发布时间】:2021-06-01 20:08:50
【问题描述】:

我需要处理我的一些数据。有一个对象数组。这些对象之一是“活动的”。我拼接了活动房间并将其放置在数组的索引 0 处。现在我需要删除更多数据并且遇到问题。将只有一个roomActive: true,只有一个sectionActive: true

const exhibitions = [
    {
        title: "Lobby",
        roomActive: false,
        sections: [
            { id: 4, sectionActive: false },
            { id: 5, sectionActive: false },
            { id: 6, sectionActive: false },
            { id: 7, sectionActive: false }
        ]
    },
    {
        title: "First Room",
        roomActive: false,
        sections: [
            { id: 8, sectionActive: false },
            { id: 9, sectionActive: false },
            { id: 10, sectionActive: false },
            { id: 11, sectionActive: false }
        ]
    },
    {
        title: "Second Room",
        roomActive: true,
        sections: [
            { id: 12, sectionActive: false },
            { id: 13, sectionActive: true },
            { id: 14, sectionActive: false },
            { id: 15, sectionActive: false }
        ]
    },
    {
        title: "Fourth Room",
        roomActive: false,
        sections: [
            { id: 16, sectionActive: false },
            { id: 17, sectionActive: false },
            { id: 18, sectionActive: false },
            { id: 19, sectionActive: false }
        ]
    }

要将 sectionActive 放在索引 0 我这样做:

  const handleTimelineClose = () => {
    const newArray = [...exhibitions]

    newArray.find((item, i) => {
      return item.roomActive === true
        && newArray.unshift(newArray.splice(i, 1)[0])
    })
    return newArray
  }

此函数将此对象置于索引 0:

 {
        title: "Second Room",
        roomActive: true,
        sections: [
            { id: 12, sectionActive: false },
            { id: 13, sectionActive: true },
            { id: 14, sectionActive: false },
            { id: 15, sectionActive: false }
        ]
    }

但我还需要操作此 roomActive: true 对象的 sections 数组。我需要找到并保留 sectionActive: true 的对象和下一个索引中的对象。但我不知道该怎么做。

 {
            title: "Second Room",
            roomActive: true,
            sections: [
                { id: 13, sectionActive: true },
                { id: 14, sectionActive: false },
            ]
        }

期望的结果:

 const exhibitions = [
        {
            title: "Second Room",
            roomActive: true,
            sections: [
                { id: 13, sectionActive: true },
                { id: 14, sectionActive: false },
            ]
        },
        {
            title: "Lobby",
            roomActive: false,
            sections: [
                { id: 4, sectionActive: false },
                { id: 5, sectionActive: false },
                { id: 6, sectionActive: false },
                { id: 7, sectionActive: false }
            ]
        },
        {
            title: "First Room",
            roomActive: false,
            sections: [
                { id: 8, sectionActive: false },
                { id: 9, sectionActive: false },
                { id: 10, sectionActive: false },
                { id: 11, sectionActive: false }
            ]
        },
        {
            title: "Fourth Room",
            roomActive: false,
            sections: [
                { id: 16, sectionActive: false },
                { id: 17, sectionActive: false },
                { id: 18, sectionActive: false },
                { id: 19, sectionActive: false }
            ]
        }

【问题讨论】:

  • .some() 不是这样工作的,应该被使用....some() 在至少一个元素的回调返回true 时返回true,否则false
  • “但我不知道该怎么做” - 使用循环或.findIndex() -> 到目前为止,你有什么尝试自己解决这个问题?
  • 确切的预期结果是什么?
  • 这和排序有什么关系?
  • 已更新以包含我想要获得的结果。谢谢!

标签: javascript arrays object splice


【解决方案1】:

您可以通过查看属性来过滤数组,并检查之前的属性是否为true

const
    sections = [{ id: 12, sectionActive: false }, { id: 13, sectionActive: true }, { id: 14, sectionActive: false }, { id: 15, sectionActive: false }],
    trueAndNext = sections
        .filter(({ sectionActive }, i, a) => sectionActive || a[i -1]?.sectionActive);
    

console.log(trueAndNext);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2019-10-16
    • 2018-12-02
    • 2017-06-11
    • 2015-04-13
    • 2021-04-28
    • 2023-01-13
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多