【发布时间】:2020-08-09 23:03:52
【问题描述】:
这是数据:
const data = {
element6: {
col: 2,
row: 3,
text: "Col2, Row1"
},
element1: {
col: 1,
row: 1,
text: "Col1, Row1"
},
element8: {
col: 3,
row: 2,
text: "Col2, Row1"
},
element2: {
col: 1,
row: 2,
text: "Col1, Row2"
},
element5: {
col: 2,
row: 2,
text: "Col2, Row2"
},
element3: {
col: 1,
row: 3,
text: "Col1, Row3"
},
element4: {
col: 2,
row: 1,
text: "Col2, Row1"
},
element7: {
col: 3,
row: 1,
text: "Col2, Row1"
},
element9: {
col: 3,
row: 3,
text: "Col2, Row1"
},
};
我想要做的是添加一个属性ind(等于元素的col值)并将col(选择的最小值)覆盖到相同row中的所有元素和不同的col。
例如,elements 2、5 和 8 在 row: 2 中,因此它们将更改为:
{
element8: {
col: 1,
row: 2,
text: "Col2, Row1",
ind: 3
},
element2: {
col: 1,
row: 2,
text: "Col1, Row2",
ind: 1
},
element5: {
col: 1,
row: 2,
text: "Col2, Row2",
ind: 2
},
}
注意,我不想更改data 中对象的顺序。
谢谢。
【问题讨论】:
标签: javascript sorting object