我尝试了以下代码,但得到一个数组,但我想创建以下输出:
var hid_col_arr = [];
if (hiddenFieldsArr.length) {
$.each(hiddenFieldsArr, function (hid_field, hid_field_label) {
hid_col_arr[hid_field] = {targets: hid_field_label.original, orderData: hid_field_label.hidden, visible: false};
});
console.log(hid_col_arr);
}
输出:
[
0: {
targets: 4,
orderData: 4,
visible: false
},
1: {
targets: 5,
orderData: 5,
visible: false
}
]
我想要输出:
{
targets: 4,
orderData: 4,
visible: false
}, {
targets: 5,
orderData: 5,
visible: false
}
答案 0 :(得分:1)
我相信您想从一个对象获取一个数组:
const output = {
0: {
targets: 4,
orderData: 4,
visible: false
},
1: {
targets: 5,
orderData: 5,
visible: false
}
};
const arr = Object.values(output).sort((a, b) => a.orderData - b.orderData);
console.log(arr);
(排序与保持对象有序相关)