[英]Sort array of objects based on two fields - Javascript
I have some data and I would like to sort the array based on two different fields and change the "order" property as well.我有一些数据,我想根据两个不同的字段对数组进行排序并更改“order”属性。 Each object has a position property with a value of "Before" and "After".
每个对象都有一个值为“Before”和“After”的位置属性。 Before should come first followed by After.
Before 应该先来,然后是 After。 Plus, the before property objects within a catgory should have a separate index and the after property object within a category should have a separate index starting at 1. I am almost done but not able to fix the order.
另外,类别中的 before 属性对象应该有一个单独的索引,类别中的 after 属性对象应该有一个从 1 开始的单独索引。我快完成了,但无法修复顺序。 Please help.
请帮忙。
const data = [{ "id": 0, "mdmStep": { "name": "Insert to Stage", "id": 1 }, "relativePosition": { "name": "After", "id": 2 }, "order": 1, "details": [{ "commandText": "SELECT * FROM MDSRC_PRO_KOMODO_TRAN" }] }, { "id": 3, "mdmStep": { "name": "Insert to Stage", "id": 1 }, "relativePosition": { "name": "Before", "id": 1 }, "order": 4, "details": [{ "commandText": "SELECT * FROM MDSRC_PRO_KOMODO_CONFIG" }] }, { "id": 1, "mdmStep": { "name": "Insert to Stage", "id": 1 }, "relativePosition": { "name": "After", "id": 2 }, "order": 2, "details": [{ "commandText": "SELECT * FROM MDSRC_PRO_KOMODO_ADDR" }] }, { "id": 2, "mdmStep": { "name": "Insert to Raw", "id": 2 }, "relativePosition": { "name": "After", "id": 2 }, "order": 3, "details": [{ "commandText": "SELECT * FROM MDSRC_PRO_KOMODO_ADDR" }] }, { "id": 4, "mdmStep": { "name": "Insert to Raw", "id": 2 }, "relativePosition": { "name": "After", "id": 2 }, "order": 5, "details": [{ "commandText": "SELECT * FROM MDSRC_PRO_KOMODO_CONFIG" }] }, { "id": 5, "mdmStep": { "name": "Update Match Fields", "id": 5 }, "relativePosition": { "name": "Before", "id": 1 }, "order": 6, "details": [{ "commandText": "HELLO ARUN" }] } ] const groups = _.groupBy(data, 'mdmStep.name') const groupValues = _.reduce(_.keys(groups), (a, c) => { a[c] = _.orderBy(groups[c], 'relativePosition.id').map((i, idx) => { if (i.relativePosition.id === 1) { return { ...i, order: idx } } return { ...i, order: idx } }); return a }, {}) const changeOrders = _.orderBy(_.flattenDeep((_.map(_.keys(groupValues), group => { return _.map(groupValues[group], (i, index) => { return { ...i, order: index || index + 1 } }) }))), 'mdmStep.id') console.log(changeOrders)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
The expected output is预期的输出是
[
{
"id": 3,
"mdmStep": {
"name": "Insert to Stage",
"id": 1
},
"relativePosition": {
"name": "Before",
"id": 1
},
"order": 1,
"details": [
{
"commandText": "SELECT * FROM MDSRC_PRO_KOMODO_CONFIG"
}
]
},
{
"id": 0,
"mdmStep": {
"name": "Insert to Stage",
"id": 1
},
"relativePosition": {
"name": "After",
"id": 2
},
"order": 1,
"details": [
{
"commandText": "SELECT * FROM MDSRC_PRO_KOMODO_TRAN"
}
]
},
{
"id": 1,
"mdmStep": {
"name": "Insert to Stage",
"id": 1
},
"relativePosition": {
"name": "After",
"id": 2
},
"order": 2,
"details": [
{
"commandText": "SELECT * FROM MDSRC_PRO_KOMODO_ADDR"
}
]
},
{
"id": 2,
"mdmStep": {
"name": "Insert to Raw",
"id": 2
},
"relativePosition": {
"name": "After",
"id": 2
},
"order": 1,
"details": [
{
"commandText": "SELECT * FROM MDSRC_PRO_KOMODO_ADDR"
}
]
},
{
"id": 4,
"mdmStep": {
"name": "Insert to Raw",
"id": 2
},
"relativePosition": {
"name": "After",
"id": 2
},
"order": 2,
"details": [
{
"commandText": "SELECT * FROM MDSRC_PRO_KOMODO_CONFIG"
}
]
},
{
"id": 5,
"mdmStep": {
"name": "Update Match Fields",
"id": 5
},
"relativePosition": {
"name": "Before",
"id": 1
},
"order": 1,
"details": [
{
"commandText": "HELLO ARUN"
}
]
}
]
In the above response, before is followed by after, and index gets reset to 1 again for after.在上面的响应中,before 后跟 after,并且 index 再次重置为 1 for after。 This is the expected behavior.
这是预期的行为。
Please advice.请指教。
In changeOrders
you increase index + 1
at each iteration, but must do in only if index === 0
, just write like this return {...i, order: index || index + 1}
在
changeOrders
您在每次迭代时增加index + 1
,但只有在index === 0
时才必须这样做,就像这样写return {...i, order: index || index + 1}
return {...i, order: index || index + 1}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.