【问题标题】:move array data to another in same object将数组数据移动到同一对象中的另一个
【发布时间】:2018-07-09 04:17:14
【问题描述】:

我想将 card1 对象移动到数组中 cards 的第二个位置。


卡1

lanes: [{
                    cards: [{
                            id: card1,
                            title: 'Write a cool JS library',
                            listId: "lane1"
                        },
                        {
                            id: card2,
                            title: 'Make it generic enough',
                            listId: "lane1"
                        }
                    ],
                    id: "lane1",
                    label: "2/2",
                    title: "title.."
                },
                {
                    cards: [
                        {
                            id: card3,
                            title: 'Write README',
                            listId: "lane2"
                        }
                    ],
                    id: "lane2",
                    label: "0/0",
                    title: "Something title.."
                }
            ]

卡片 2

    lanes: [{
            cards: [{
                    id: card1,
                    title: 'Write a cool JS library',
                    listId: "lane1"
                },
                {
                    id: card2,
                    title: 'Make it generic enough',
                    listId: "lane1"
                }
            ],
            id: "lane1",
            label: "2/2",
            title: "title.."
        },
        {
            cards: [
                {
                    id: card3,
                    title: 'Write README',
                    listId: "lane2"
                }
            ],
            id: "lane2",
            label: "0/0",
            title: "Something title.."
        }
    ]

【问题讨论】:

    标签: javascript arrays json sorting object


    【解决方案1】:

    您可以使用splice() 从数组中获取对象,并使用push() 将其添加到另一个数组中。

    const data = {"lanes":[{"cards":[{"id":"card1","title":"Write a cool JS library","listId":"lane1"},{"id":"card2","title":"Make it generic enough","listId":"lane1"}],"id":"lane1","label":"2/2","title":"title.."},{"cards":[{"id":"card3","title":"Write README","listId":"lane2"}],"id":"lane2","label":"0/0","title":"Something title.."}]}
    
    data.lanes[1].cards.push(...data.lanes[0].cards.splice(0, 1))
    console.log(data)

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 2017-07-11
      • 2015-10-31
      • 1970-01-01
      • 2017-01-15
      相关资源
      最近更新 更多