将数组数据移动到同一对象中的另一个

时间:2018-01-30 10:05:40

标签: javascript arrays json sorting object

  

我想将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.."
        }
    ]

1 个答案:

答案 0 :(得分: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)