I want to move an element to first position from an inner array like below
{
"activeContainerId": "955bd18f-6561-44b4-ba8f-7094e4e183b8",
"amendCartContainers": [],
"data": [
{
"id": "955bd18f-6561-44b4-ba8f-7094e4e183b8",
"categoryName": "My_Category_Name",
"categoryId": "cat_spc_my_category",
"sections": [
{
"id": "cat_spc_my_id",
"title": "APPLE",
"items": [
{
"id": "a6d89ee2-6832-43e4-85ea-6ec541f06c36",
"action": "add",
"title": "Apple iPhone 11",
"characteristics": [
----
],
"promotions": [],
"quantity": 1,
"oneTimePrice": [
{
----
}
],
"recurringPrice": [],
"totalOneTimePrice": [
{
----
}
],
}
]
},
{
"id": "cat_spc_my_id2",
"title": "My_Title2",
"items": [
{
"id": "0ea7cade-96e0-4e5d-8baf-ace2f7ce123f",
"action": "add",
"title": "My Inner title2",
"characteristics": [],
"promotions": [],
"quantity": 1,
"addedBySCRuleEngine": true,
"oneTimePrice": [
{
----
}
],
"recurringPrice": [],
"totalOneTimePrice": [
{
----
}
],
"totalPriceRecurring": []
}
]
},
{
"id": "cat_spc_my_special_id",
"title": "my_special_title",
"items": [
{
"id": "870d3871-6a0c-42f2-b123-9d75a3f3e3ee",
"action": "add",
"title": "my inner title3",
"characteristics": [],
"promotions": [
{
----
}
],
"quantity": 1,
"itemTerm": [
{
"duration": {
"amount": 12,
"units": "Month"
}
}
],
"oneTimePrice": [
{
-----
}
],
"recurringPrice": [
{
-----
}
],
"totalOneTimePrice": [
{
-----
}
],
"totalPriceRecurring": [
{
-----
}
]
}
]
},
{
"id": "cat_spc_my_id4",
"title": "my_title4",
"items": [
{
"id": "1011730c-d8d2-45ac-8ee2-98f981184a85",
"action": "add",
"title": "my_title4",
"characteristics": [],
"promotions": [],
"quantity": 1,
"oneTimePrice": [
{
-----
}
}
]
}
],
"totalPriceOneTime": [
{
----
}
],
"totalPriceRecurring": [
{
----
}
],
"promotions": [],
"notes": [],
"processContext": "ACQ",
"visibilityInSection": {
"id": "bo_lov_visibilityinsection_section1",
"name": "Section 1",
"visibilityOrder": 1
}
}
],
"applyCouponStatus": null,
"promotions": [],
"totalPriceOneTime": [
{
"dutyFreeAmount": {
"unit": "price_unit",
"value": 479.68
},
"taxIncludedAmount": {
"unit": "price_unit",
"value": 580.41
},
"dutyFreeAlteredAmount": {
"unit": "price_unit",
"value": 479.68
},
"taxIncludedAlteredAmount": {
"unit": "price_unit",
"value": 580.41
},
"taxAmount": 100.73,
"amountPayable": {
"unit": "price_unit",
"value": 580.41
}
}
],
"totalPriceRecurring": [
{
"dutyFreeAmount": {
"unit": "price_unit",
"value": 41.32
},
"taxIncludedAmount": {
"unit": "price_unit",
"value": 50
},
"dutyFreeAlteredAmount": {
"unit": "price_unit",
"value": 24.79
},
"taxIncludedAlteredAmount": {
"unit": "price_unit",
"value": 30
},
"taxAmount": 5.21,
"amountPayable": {
"unit": "price_unit",
"value": 30
}
}
]
}
There are 4 items currently in the above array and I want to filter and move the id ‘cat_spc_my_special_id’ from ‘data.sections to the top of list. Keyword that I want to use is ‘special’ in this case.
I have tried below
Try1:
let filterKey = 'special';
const shoppingBagNew = shoppingBag?.data?.
[0]?.sections.filter(function(x,y){
return x == filterKey ? -1 : y == filterKey ? 1 : 0;
})
Try2:
const sections = shoppingBag?.data?.[0]?.sections;
const RedIndex = shoppingBag?.data?.[0]?.sections.findIndex(section =>
section.name == 'special');
shoppingBag?.data?.[0]?.sections.push(...sections.splice(0, RedIndex));
console.log('shoppingBag after filter',shoppingBag);
I knew that moving to the top of list is possible for only normal array of items which is not a nested array but Is this possible for a nested array like above? Any help would be appreciated..! Thanks.