我正在研究JavaScript功能,我正在努力工作。
我有2个数组。
数组1保持单个记录,数组2保持说例3记录。我想在array1记录的相同索引处推送数组1中的数组2的三个记录...
QualificationElemenetsAndComponents[0] = {
Element [
ElementIndex : "",
ElementMarkingSchemeTitle : "",
ElementAvailableMark: "",
ElementPassMark: "",
ElementDistinctionMark: "",
Component[0]= [
componentIndex="",
componentMark =""
],
Component[1]= [
componentIndex="",
componentMark =""
],
Component[2]= [
componentIndex="",
componentMark =""
],
}
<script type="text/javascript">
var selectedComponentList = [];
var selectElementList = [];
var QualificationElemenetsAndComponentsList = [];
selectElementList = {
ElementIndex: 1,
ElementMarkingSchemeTitle: “markingScheme1”,
ElementAvailableMark: 100,
ElementPassMark: 50,
ElementMeritMark: 60,
ElementDistinctionMark: 70
};
selectedComponentList = {
componentIndex: 1,
componentMark: 80
};
selectedComponentList = {
componentIndex: 23,
componentMark: 50
};
selectedComponentList = {
componentIndex: 3,
componentMark: 10
};
$(document).ready(function () {
}); //end JQuery
</script>
答案 0 :(得分:1)
实际上,您必须使用Array.prototype.splice方法。
z = [1,2,3];
z.splice(1,0,6,7,8,9);
console.log(z); // [1, 6, 7, 8, 9, 2, 3]
答案 1 :(得分:1)
尝试以下
var QualificationElemenetsAndComponentsList = [];
var selectElementList = {//this is a javascript object
ElementIndex: 1,
ElementMarkingSchemeTitle: "markingScheme1",
ElementAvailableMark: 100,
ElementPassMark: 50,
ElementMeritMark: 60,
ElementDistinctionMark: 70
};
selectedComponentList = [];
selectedComponentList[selectedComponentList.length] = {
componentIndex: 1,
componentMark: 80
};
selectedComponentList[selectedComponentList.length] = {
componentIndex: 23,
componentMark: 50
};
selectedComponentList[selectedComponentList.length] = {
componentIndex: 3,
componentMark: 10
};
selectElementList.Component = selectedComponentList; //Here's how you assign the array to the object property
QualificationElemenetsAndComponentsList[QualificationElemenetsAndComponentsList.length] = selectElementList;
答案 2 :(得分:0)
我已成功解决,请告诉我是否正确......
selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark });
selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });
selectElementList.push({ Components: selectedComponentList });
QualificationElemenetsAndComponentsList.push({ Element: selectElementList });
selectElementList = [];
selectedComponentList = [];
并按照每个元素重复循环
以下代码生成JSON输出
var myElementList = "";
myElementList = JSON.stringify({ QualificationElemenetsAndComponentsList: QualificationElemenetsAndComponentsList });
document.write("JSPN >>>> " + myElementList);
JSPN >>>> {"QualificationElemenetsAndComponentsList":[{"Element":[{"ElementIndex":1,"ElementMarkingSchemeTitle":"m1","ElementAvailableMark":"10","ElementPassMark":"55","ElementMeritMark":"44","ElementDistinctionMark":"4"},{"Components":[{"componentIndex":1,"componentMark":"2"},{"componentIndex":2,"componentMark":"23"},{"componentIndex":3,"componentMark":"55"}]}]},{"Element":[{"ElementIndex":3,"ElementMarkingSchemeTitle":"m2","ElementAvailableMark":"45","ElementPassMark":"545","ElementMeritMark":"444","ElementDistinctionMark":"4"},{"Components":[{"componentIndex":2,"componentMark":"45"},{"componentIndex":4,"componentMark":"52"}]}]},{"Element":[{"ElementIndex":8,"ElementMarkingSchemeTitle":"m3","ElementAvailableMark":"100","ElementPassMark":"224","ElementMeritMark":"455","ElementDistinctionMark":"54"},{"Components":[{"componentIndex":2,"componentMark":"4"},{"componentIndex":1,"componentMark":"69"}]}]}]}