简体   繁体   English

我想将数组值转换为单个对象

[英]I want to convert array values to single object

I have tried following code and I get an array but I want to create the following output:我尝试了以下代码并得到了一个数组,但我想创建以下输出:

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);
}

output:输出:

[
  0: {
    targets: 4, 
    orderData: 4, 
    visible: false
  }, 
  1: {
    targets: 5, 
    orderData: 5, 
    visible: false
  }
]

I want output:我想要输出:

{
  targets: 4, 
  orderData: 4, 
  visible: false
}, {
  targets: 5, 
  orderData: 5, 
  visible: false
}

I believe you want to get an array from an object:我相信你想从一个对象中获取一个数组:

 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);

(The sorting is relevant to keep the objects in order) (排序与保持对象有序有关)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM