I am working with an array of objects:
const array = [
{
id: "5a2524432b68c725c06ac987",
customOrder: 1,
name: "One",
},
{
id: "5a2524432b68sgs25c06ac987",
customOrder: 2,
name: "Two",
},
{
id: "5a252wfew32b68c725c06ac987",
customOrder: 3,
name: "Three",
},
{
id: "5a25gffe32b68c725c06ac987",
customOrder: 4,
name: "Four",
},
{
id: "5a2524432b68c725c06acfee7",
customOrder: 5,
name: "Five",
},
{
id: "5a2524432b68c725c06ac556",
customOrder: 6,
name: "Six",
},
]
If I update the customOrder
property of one object and need to adjust the customOrder
values of the other elements accordingly, for example changing index 2 to have a custom order of 4, resulting in:
const array = [
{
id: "5a2524432b68c725c06ac987",
customOrder: 1,
name: "One",
},
{
id: "5a2524432b68sgs25c06ac987",
customOrder: 2,
name: "Two",
},
{
id: "5a25gffe32b68c725c06ac987",
customOrder: 3,
name: "Four",
},
{
id: "5a252wfew32b68c725c06ac987",
customOrder: 4,
name: "Three",
}
{
id: "5a2524432b68c725c06acfee7",
customOrder: 5,
name: "Five",
},
{
id: "5a2524432b68c725c06ac556",
customOrder: 6,
name: "Six",
},
]
I'm pondering over using methods like array.slice()
or possibly utilizing lodash's _.putAt()
, but I am exploring simpler approaches to achieve the desired outcome.