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