删除一个对象并按顺序重命名所有对象名称。

huangapple go评论71阅读模式
英文:

Remove object one object and rename all object name sequence wise

问题

object

这是我检索到的对象。如何移除 module_1 对象并重命名 module 对象,例如移除 module_1 并将 module_2、module_3... 重命名为 module_1、module_2...。

{
  "module_1": {
    "modulename": "maths",
    "moduletypes": "mandatory",
    "moduleid": "88",
    "moduleaim": "MODULE AIM",
    "learningobjectives": "LEARNING OBJECTIVES",
    "assessmentcriteria": "ASSESSMENT CRITERIA",
    "scenario": "sfgsfgsdfg",
    "tasknumber": 2,
    "taskarea": [
      "<p>taskarea1sdfgsdfsfgagagasdasdf</p>",
      "<p>asdasdasd</p>"
    ],
    "librarylinks": [
      "asdasdsa",
      "asdasd"
    ],
    "studylinks": [
      "asdasd",
      "asdasd"
    ],
    "videoslinks": [
      "asdasdfsdfsdfsdfsdfsdfsdf",
      "asdasdfsfsdfsf"
    ]
  },
  "module_2": {
    "modulename": "asasdasd",
    "moduletypes": "optional",
    "moduleid": "23443",
    "moduleaim": "asdasgdfg",
    "learningobjectives": "dgdg",
    "assessmentcriteria": "dfgdg",
    "scenario": "dgdgdg",
    "tasknumber": 1,
    "taskarea": [
      "dgdfgdfg"
    ],
    "librarylinks": [
      "dfgdfgdfg"
    ],
    "studylinks": [
      "dgdfgdfg"
    ],
    "videoslinks": [
      "dgdfgdfg",
      "sdfasdfasf"
    ]
  },
  "module_3": {
    "modulename": "science",
    "moduletypes": "optional",
    "moduleid": "3234",
    "moduleaim": "sadfsdfgsdaf",
    "learningobjectives": "asfdsadf",
    "assessmentcriteria": "asdfadsf",
    "scenario": "asdfasdfasf",
    "tasknumber": 1,
    "taskarea": [
      "adfafads"
    ],
    "librarylinks": [
      "afdasfasdf"
    ],
    "studylinks": [
      "asdfasafs"
    ],
    "videoslinks": [
      "afdaafd",
      "asfdasfasf"
    ]
  }
}

html

这是我的 HTML 代码。

<button
  class="btn rounded-pill btn-primary remove_module remove_module_button"
  data-id="5"
  data-modulename="module_1"
  type="button"
>
  移除模块 <i class="bx bx-x-circle ml-1"></i>
</button>

jquery

这是我的 jQuery 代码。

$(document).on('click', '.remove_module', function() {
	var id = $(this).data('id');
	var modulename = $(this).data('modulename');
	var ok = confirm("确定要删除此模块吗?");
	if (ok) {
		$.ajax({
			method: "GET",
			url: "deletemodule",
			data: {
				id: id,
				modulename: modulename
			},
			success: function(data) {
				if (data == 1) {
					alert("课程删除成功!");
					window.location.reload();
				}
			}
		});
	}
});

php

这是我的 PHP 代码。

public function deleteModule(Request $request){
        $id =  $request->id;
        $modulename =  $request->modulename;
        $data = Course::where('id', $id)->get();
        $modueldata = $data[0]->modules; //获取 modules 列中的模块数据
     }

我需要删除用户提供的对象并按顺序重命名所有模块。

这是我需要执行的操作。

{
    "module_1": {  
        "modulename": "asasdasd",
        "moduletypes": "optional",
        "moduleid": "23443",
        "moduleaim": "asdasgdfg",
        "learningobjectives": "dgdg",
        "assessmentcriteria": "dfgdg",
        "scenario": "dgdgdg",
        "tasknumber": 1,
        "taskarea": [
            "dgdfgdfg"
        ],
        "librarylinks": [
            "dfgdfgdfg"
        ],
        "studylinks": [
            "dgdfgdfg"
        ],
        "videoslinks": [
            "dgdfgdfg",
            "sdfasdfasf"
        ]
    },
    "module_2": { 
        "modulename": "science",
        "moduletypes": "optional",
        "moduleid": "3234",
        "moduleaim": "sadfsdfgsdaf",
        "learningobjectives": "asfdsadf",
        "assessmentcriteria": "asdfadsf",
        "scenario": "asdfasdfasf",
        "tasknumber": 1,
        "taskarea": [
            "adfafads"
        ],
        "librarylinks": [
            "afdasfasdf"
        ],
        "studylinks": [
            "asdfasafs"
        ],
        "videoslinks": [
            "afdaafd",
            "asfdasfasf"
        ]
    }
}
英文:

object

This my retrieved object. how can i remove module_1 object and rename module object ex. remove module_1 and rename module_2, module_3... to module_1 module_2....

`{
&quot;module_1&quot;: {
&quot;modulename&quot;: &quot;maths&quot;,
&quot;moduletypes&quot;: &quot;mandatory&quot;,
&quot;moduleid&quot;: &quot;88&quot;,
&quot;moduleaim&quot;: &quot;MODULE AIM&quot;,
&quot;learningobjectives&quot;: &quot;LEARNING OBJECTIVES&quot;,
&quot;assessmentcriteria&quot;: &quot;SSESSMENT CRITERIA&quot;,
&quot;scenario&quot;: &quot;sfgsfgsdfg&quot;,
&quot;tasknumber&quot;: 2,
&quot;taskarea&quot;: [
&quot;&lt;p&gt;taskarea1sdfgsdfsfgagagasdasdf&lt;/p&gt;&quot;,
&quot;&lt;p&gt;asdasdasd&lt;/p&gt;&quot;
],
&quot;librarylinks&quot;: [
&quot;asdasdsa&quot;,
&quot;asdasd&quot;
],
&quot;studylinks&quot;: [
&quot;asdasd&quot;,
&quot;asdasd&quot;
],
&quot;videoslinks&quot;: [
&quot;asdasdfsdfsdfsdfsdfsdfsdf&quot;,
&quot;asdasdfsfsdfsf&quot;
]
},
&quot;module_2&quot;: {
&quot;modulename&quot;: &quot;asasdasd&quot;,
&quot;moduletypes&quot;: &quot;optional&quot;,
&quot;moduleid&quot;: &quot;23443&quot;,
&quot;moduleaim&quot;: &quot;asdasgdfg&quot;,
&quot;learningobjectives&quot;: &quot;dgdg&quot;,
&quot;assessmentcriteria&quot;: &quot;dfgdg&quot;,
&quot;scenario&quot;: &quot;dgdgdg&quot;,
&quot;tasknumber&quot;: 1,
&quot;taskarea&quot;: [
&quot;dgdfgdfg&quot;
],
&quot;librarylinks&quot;: [
&quot;dfgdfgdfg&quot;
],
&quot;studylinks&quot;: [
&quot;dgdfgdfg&quot;
],
&quot;videoslinks&quot;: [
&quot;dgdfgdfg&quot;,
&quot;sdfasdfasf&quot;
]
},
&quot;module_3&quot;: {
&quot;modulename&quot;: &quot;science&quot;,
&quot;moduletypes&quot;: &quot;optional&quot;,
&quot;moduleid&quot;: &quot;3234&quot;,
&quot;moduleaim&quot;: &quot;sadfsdfgsdaf&quot;,
&quot;learningobjectives&quot;: &quot;asfdsadf&quot;,
&quot;assessmentcriteria&quot;: &quot;asdfadsf&quot;,
&quot;scenario&quot;: &quot;asdfasdfasf&quot;,
&quot;tasknumber&quot;: 1,
&quot;taskarea&quot;: [
&quot;adfafads&quot;
],
&quot;librarylinks&quot;: [
&quot;afdasfasdf&quot;
],
&quot;studylinks&quot;: [
&quot;asdfasafs&quot;
],
&quot;videoslinks&quot;: [
&quot;afdaafd&quot;,
&quot;asfdasfasf&quot;
]
}
}
`

html

Here is my html code

`&lt;button
class=&quot;btn rounded-pill btn-primary remove_module remove_module_button&quot;
data-id=&quot;5&quot;
data-modulename=&quot;module_1&quot;
type=&quot;button&quot;
&gt;
Remove Module &lt;i class=&quot;bx bx-x-circle ml-1&quot;&gt;&lt;/i&gt;
&lt;/button&gt;`

jquery

here is my jquery code

`$(document).on(&#39;click&#39;, &#39;.remove_module&#39;, function() {
var id = $(this).data(&#39;id&#39;);
var modulename = $(this).data(&#39;modulename&#39;);
var ok = confirm(&quot;Are you sure, you want to delete this Module?&quot;);
if (ok) {
$.ajax({
method: &quot;GET&quot;,
url: &quot;deletemodule&quot;,
data: {
id: id,
modulename: modulename
},
success: function(data) {
if (data == 1) {
alert(&quot;Course Deleted Successfully!&quot;);
window.location.reload();
}
}
});
}
});`

php

Here is my php code

`public function deleteModule(Request $request){
$id =  $request-&gt;id;
$modulename =  $request-&gt;modulename;
$data = Course::where(&#39;id&#39;, $id)-&gt;get();
$modueldata = $data[0]-&gt;modules; //get module data in modules column
}`

I need to remove object given by user and rename all module sequence wise.

`{
&quot;module_1&quot;: {  
&quot;modulename&quot;: &quot;asasdasd&quot;,
&quot;moduletypes&quot;: &quot;optional&quot;,
&quot;moduleid&quot;: &quot;23443&quot;,
&quot;moduleaim&quot;: &quot;asdasgdfg&quot;,
&quot;learningobjectives&quot;: &quot;dgdg&quot;,
&quot;assessmentcriteria&quot;: &quot;dfgdg&quot;,
&quot;scenario&quot;: &quot;dgdgdg&quot;,
&quot;tasknumber&quot;: 1,
&quot;taskarea&quot;: [
&quot;dgdfgdfg&quot;
],
&quot;librarylinks&quot;: [
&quot;dfgdfgdfg&quot;
],
&quot;studylinks&quot;: [
&quot;dgdfgdfg&quot;
],
&quot;videoslinks&quot;: [
&quot;dgdfgdfg&quot;,
&quot;sdfasdfasf&quot;
]
},
&quot;module_2&quot;: { 
&quot;modulename&quot;: &quot;science&quot;,
&quot;moduletypes&quot;: &quot;optional&quot;,
&quot;moduleid&quot;: &quot;3234&quot;,
&quot;moduleaim&quot;: &quot;sadfsdfgsdaf&quot;,
&quot;learningobjectives&quot;: &quot;asfdsadf&quot;,
&quot;assessmentcriteria&quot;: &quot;asdfadsf&quot;,
&quot;scenario&quot;: &quot;asdfasdfasf&quot;,
&quot;tasknumber&quot;: 1,
&quot;taskarea&quot;: [
&quot;adfafads&quot;
],
&quot;librarylinks&quot;: [
&quot;afdasfasdf&quot;
],
&quot;studylinks&quot;: [
&quot;asdfasafs&quot;
],
&quot;videoslinks&quot;: [
&quot;afdaafd&quot;,
&quot;asfdasfasf&quot;
]
}
}`

this how i need to do.

答案1

得分: 0

这只涉及数组处理和取消设置。

$modules = $data[0]->modules; // 如果它不是数组,也要进行转换
$moduleToRemove = $request->modulename;
$filteredArray = array_filter($modules, fn($module) => ($module !== $moduleToRemove), ARRAY_FILTER_USE_KEY);
$finalModule = [];
$i = 1;
foreach ($filteredArray as $moduleData) {
    $finalModule['module_'.$i] = $moduleData;
    $i++;
}
英文:

It is just array handling and unset.

$modules        = $data[0]-&gt;modules; //this is array if not convert it too
$moduleToRemove = $request-&gt;modulename;
$filteredArray  = array_filter($modules, fn($module) =&gt; ($module !== $moduleToRemove), ARRAY_FILTER_USE_KEY);
$finalModule    = [];
$i = 1;
foreach ($filteredArray as $moduleData) {
$finalModule[&#39;module_&#39;.$i] = $moduleData;
$i++;
}

huangapple
  • 本文由 发表于 2023年1月9日 13:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定