我想将元素从内部数组移动到第一个位置,如下所示
{
"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
}
}
]
}
上面的数组中当前有 4 个项目,我想过滤并将 id 'cat_spc_my_special_id' 从 'data.sections 移动到列表顶部。在这种情况下,我想使用的关键字是“特殊”。
我已经尝试过以下
尝试1:
let filterKey = 'special';
const shoppingBagNew = shoppingBag?.data?.
[0]?.sections.filter(function(x,y){
return x == filterKey ? -1 : y == filterKey ? 1 : 0;
})
尝试2:
const sections = shoppingBag?.data?.[0]?.sections;
const RedIndex = shoppingBag?.data?.[0]?.sections.findIndex(section =>
section.id == 'special');
shoppingBag?.data?.[0]?.sections.push(...sections.splice(0, RedIndex));
console.log('shoppingBag after filter',shoppingBag);
我知道只有普通的项目数组才可能移动到列表顶部,而这不是嵌套数组,但是对于像上面这样的嵌套数组来说这可能吗?任何帮助,将不胜感激..!谢谢。
.filter()
不对数组进行排序。您需要使用 .sort()
来代替:
const shoppingBag={"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":["something else"],"promotions":[],"quantity":1,"oneTimePrice":[{"something":"else"}],"recurringPrice":[],"totalOneTimePrice":[{"something":"else"}]}]},{"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":[{"something":"else"}],"recurringPrice":[],"totalOneTimePrice":[{"something":"else"}],"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":[{"something":"else"}],"quantity":1,"itemTerm":[{"duration":{"amount":12,"units":"Month"}}],"oneTimePrice":[{"something":"else"}],"recurringPrice":[{"something":"else"}],"totalOneTimePrice":[{"something":"else"}],"totalPriceRecurring":[{"something":"else"}]}]},{"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":[{"something":"else"}]}]}],"totalPriceOneTime":[{"something":"else"}],"totalPriceRecurring":[{"something":"else"}],"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}}]};
shoppingBag.data[0].sections.sort((a,b)=>
a.id.includes("special")? -1 : b.id.includes("special") ? 1 : a.id.localeCompare(b.id)
);
// list ids of result in array:
console.log(shoppingBag.data[0].sections.map(({id})=>id))