删除元素表单对象并重新索引并添加新元素

时间:2018-11-27 13:25:15

标签: javascript php jquery

我想删除0索引元素,我想重新索引并添加我创建的新元素

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
 var offline_rijksmuseum_adult_barcode_array = {};
<?php
$data['2018']= ['852','896','787'];
$data['2019']= ['185','208'];
foreach($data as $key => $result) {
 ?>
 var sub_array = {};
 <?php
 foreach($result as $keyz => $r) { ?>   
    sub_array['<?php echo $keyz; ?>'] = "<?php echo $r; ?>";
<?php
 }
 ?> 
    offline_rijksmuseum_adult_barcode_array['<?php echo $key; ?>'] = sub_array;
<?php
 } ?>

console.log(offline_rijksmuseum_adult_barcode_array);

2018:{0: "852", 1: "896", 2: "787"}
2019:{0: "185", 1: "208"}

我想要删除0索引元素和reindx,并在想要输出的末尾添加insert new元素

   2018:{0: "896", 1: "787", 2: "6985"}
   2019:{0: "208", 1: "209"}

1 个答案:

答案 0 :(得分:0)

如果要处理索引,请始终使用数组数据类型。我在下面的代码片段中对输出做了一些更改,这应该适合您的情况-

let data = {
  2018: ["896", "787", "6985"] //changed to array intead of object
};

//to remove index 0, with auto reindexing
data[2018].shift();
console.log(data);


//to append at the end
data[2018].push("111");
console.log(data);