如何在Javascript中从中到后放置一些数组元素

时间:2017-12-08 16:45:53

标签: javascript arrays

我有以下JS对象数组,其元素只是HTML表的列:

var my_cols = [
  {
    "id": "name",
    "header": [
      "Name",
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "type",
    "header": [
      "Type",
      {
        "content": "textFilter"
      }
    ],
    "width": 180,
    "sort": "string"
  },
  {
    "id": "dob",
    "header": [
      "DOB",
      {
        "content": "textFilter"
      }
    ],
    "width": 100,
    "sort": "string"
  },
  {
    "id": "merged_a",
    "header": [
      {
        "text": "merged_a",
        "colspan": 3
      },
      {
        "text": "port"
      },
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "merged_b",
    "header": [
      null,
      {
        "text": "merged_b"
      },
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "merged_c",
    "header": [
      null,
      {
        "text": "merged_c"
      },
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "place",
    "header": [
      "place",
      {
        "content": "textFilter"
      }
    ],
    "width": 300,
    "sort": "string"
  },
  {
    "id": "address",
    "header": [
      "address",
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "phone",
    "header": [
      "phone",
      {
        "content": "textFilter"
      }
    ],
    "width": 200,
    "sort": "string"
  }
];

我想将ID为' merged_a' ,' merged_b'和' merged_c'在整个数组my_cols的末尾。到目前为止,我已经编写了下面的代码,但它没有给我确切的结果:

var my_cols= [{"id":"name","header":["Name",{"content":"textFilter"}],"width":200,"sort":"string"},
{"id":"type","header":["Type",{"content":"textFilter"}],"width":180,"sort":"string"},
{"id":"dob","header":["DOB",{"content":"textFilter"}],"width":100,"sort":"string"},
{"id":"merged_a","header":[{"text":"merged_a","colspan":3},{"text":"port"},{"content":"textFilter"}],"width":200,"sort":"string"},
{"id":"merged_b","header":[null,{"text":"merged_b"},{"content":"textFilter"}],"width":200,"sort":"string"},
{"id":"merged_c","header":[null,{"text":"merged_c"},{"content":"textFilter"}],"width":200,"sort":"string"},
{"id":"place","header":["place",{"content":"textFilter"}],"width":300,"sort":"string"},
{"id":"address","header":["address",{"content":"textFilter"}],"width":200,"sort":"string"},
{"id":"phone","header":["phone",{"content":"textFilter"}],"width":200,"sort":"string"}];

我的努力



 var my_cols = [
   { "id": "name", "header": [ "Name", { "content": "textFilter" } ], "width": 200, "sort": "string" }, 
   { "id": "type", "header": [ "Type", { "content": "textFilter" } ], "width": 180, "sort": "string" }, 
   { "id": "dob", "header": [ "DOB", { "content": "textFilter" } ], "width": 100, "sort": "string" }, 
   { "id": "merged_a", "header": [{ "text": "merged_a", "colspan": 3 }, { "text": "port" }, { "content": "textFilter" } ], "width": 200, "sort": "string" }, 
   { "id": "merged_b", "header": [ null, { "text": "merged_b" }, { "content": "textFilter" } ], "width": 200, "sort": "string" }, 
   { "id": "merged_c", "header": [ null, { "text": "merged_c" }, { "content": "textFilter" } ], "width": 200, "sort": "string" }, 
   { "id": "place", "header": [ "place", { "content": "textFilter" } ], "width": 300, "sort": "string" }, 
   { "id": "address", "header": [ "address", { "content": "textFilter" } ], "width": 200, "sort": "string" },
   { "id": "phone", "header": [ "phone", { "content": "textFilter" } ], "width": 200, "sort": "string" } 
 ];

for (var i in my_cols) {

  var col = my_cols[i];
  var a_col;
  var b_col;
  var c_col;
  if (col.hasOwnProperty('id')) {
    if (col['id'] == 'merged_a') {
      a_col = my_cols.splice(i, 1);
    }
    if (col['id'] == 'merged_b') {
      b_col = my_cols.splice(i, 1);
    }
    if (col['id'] == 'merged_c') {
      c_col = my_cols.splice(i, 1);
    }
  }

}

my_cols.push(a_col);
my_cols.push(b_col);
my_cols.push(c_col);

console.log("my_cols = " + JSON.stringify(my_cols));




3 个答案:

答案 0 :(得分:0)

你可以使用array#filter两次和forEach方法来获得结果。首先得到

var my_cols = [{
    "id": "name",
    "header": ["Name", {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "type",
    "header": ["Type", {
      "content": "textFilter"
    }],
    "width": 180,
    "sort": "string"
  },
  {
    "id": "dob",
    "header": ["DOB", {
      "content": "textFilter"
    }],
    "width": 100,
    "sort": "string"
  },
  {
    "id": "merged_a",
    "header": [{
      "text": "merged_a",
      "colspan": 3
    }, {
      "text": "port"
    }, {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "merged_b",
    "header": [null, {
      "text": "merged_b"
    }, {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "merged_c",
    "header": [null, {
      "text": "merged_c"
    }, {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "place",
    "header": ["place", {
      "content": "textFilter"
    }],
    "width": 300,
    "sort": "string"
  },
  {
    "id": "address",
    "header": ["address", {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  },
  {
    "id": "phone",
    "header": ["phone", {
      "content": "textFilter"
    }],
    "width": 200,
    "sort": "string"
  }
];
// will return a new array without objects having id as merged_a,merged_b,merged_c
var withOutMergedId = my_cols.filter(function(a) {
    return "merged_a" !== a.id && "merged_b" !== a.id && "merged_c" !== a.id;
  }),
// // will return a new array with objects having only id as merged_a,merged_b,merged_c
  withMergedId = my_cols.filter(function(a) {
    return "merged_a" === a.id || "merged_b" === a.id || "merged_c" === a.id;
  });
  // using forEach to push the elements
withMergedId.forEach(function(a) {
  withOutMergedId.push(a);
});
console.log(withOutMergedId)

答案 1 :(得分:0)

您可以使用Array.prototype.sort()。它采用可选的compareFunction,可用于添加自定义排序。请参阅以下示例。

var my_cols=[{"id":"name","header":["Name",{"content":"textFilter"}],"width":200,"sort":"string"},{"id":"type","header":["Type",{"content":"textFilter"}],"width":180,"sort":"string"},{"id":"dob","header":["DOB",{"content":"textFilter"}],"width":100,"sort":"string"},{"id":"merged_a","header":[{"text":"merged_a","colspan":3},{"text":"port"},{"content":"textFilter"}],"width":200,"sort":"string"},{"id":"merged_b","header":[null,{"text":"merged_b"},{"content":"textFilter"}],"width":200,"sort":"string"},{"id":"merged_c","header":[null,{"text":"merged_c"},{"content":"textFilter"}],"width":200,"sort":"string"},{"id":"place","header":["place",{"content":"textFilter"}],"width":300,"sort":"string"},{"id":"address","header":["address",{"content":"textFilter"}],"width":200,"sort":"string"},{"id":"phone","header":["phone",{"content":"textFilter"}],"width":200,"sort":"string"}]

my_cols.sort(function(a, b) {
  var range = ['merged_a', 'merged_b', 'merged_c'];
  if (range.indexOf(a.id) > -1 && range.indexOf(b.id) <= -1) {
    return 1
  } else {
    return 0;
  }
});

console.log(my_cols);

答案 2 :(得分:0)

也许不是最优雅的方式,但要做好这份工作。

问题是当你splice数组时,索引会造成伤害。这就是你没有得到merged_b的原因。从周期中移出var a_col...

  var my_cols = [
    {"id": "name", "header": ["Name", {"content": "textFilter"}], "width": 200, "sort": "string"},
    {"id": "type", "header": ["Type", {"content": "textFilter"}], "width": 180, "sort": "string"},
    {"id": "dob", "header": ["DOB", {"content": "textFilter"}], "width": 100, "sort": "string"},
    {"id": "merged_a", "header": [{"text": "merged_a", "colspan": 3}, {"text": "port"}, {"content": "textFilter"}], "width": 200, "sort": "string"},
    {"id": "merged_b", "header": [null, {"text": "merged_b"}, {"content": "textFilter"}], "width": 200, "sort": "string"},
    {"id": "merged_c", "header": [null, {"text": "merged_c"}, {"content": "textFilter"}], "width": 200, "sort": "string"},
    {"id": "place", "header": ["place", {"content": "textFilter"}], "width": 300, "sort": "string"},
    {"id": "address", "header": ["address", {"content": "textFilter"}], "width": 200, "sort": "string"},
    {"id": "phone", "header": ["phone", {"content": "textFilter"}], "width": 200, "sort": "string"}
  ];

  var a_col;
  var b_col;
  var c_col;

  var indexes = {};

  for (var i in my_cols) {
    var col = my_cols[i];
    if (col.hasOwnProperty('id')) {
      if (col['id'] === 'merged_a') {
        a_col = my_cols[i];
        indexes.a = i;
      }
      if (col['id'] === 'merged_b') {
        b_col = my_cols[i];
        indexes.b = i;
      }
      if (col['id'] === 'merged_c') {
        c_col = my_cols[i];
        indexes.c = i;
      }
    }
  }

  indexes = dict_reverse(indexes)
  for (i in indexes) {
     my_cols.splice(indexes[i],1);  
  }

  my_cols.push(a_col);
  my_cols.push(b_col);
  my_cols.push(c_col);

  console.log(my_cols);

  function dict_reverse(obj) {
    new_obj = {}
    rev_obj = Object.keys(obj).reverse();
    rev_obj.forEach(function (i) {
      new_obj[i] = obj[i];
    });
    return new_obj;
  }