简体   繁体   中英

add javaScript array in another array

i got javaScript array hold elements records with unique elementIndex in it now what I have to add single or multiple components in same javaScript array for that particular element (same elementIdex).

this array can have as many elements as required and each element may have one or multiple components associated to that element.

I have managed to do first part, how i do second part ... that is add components records associated to single element.

note element and component are in separate javaScript function but i have global array

this is what i want to achieve (may be JSON)

   QualificationElemenetsAndComponents[0] = {

         Element [
                 ElementIndex : "",
                 ElementMarkingSchemeTitle : "",
                 ElementAvailableMark: "",
                 ElementPassMark: "",
                 ElementDistinctionMark: "",
                 Component[0]= [ 
                               componentIndex="", 
                               componentMark =""
                               ], 
                 Component[1]= [ 
                               componentIndex="", 
                               componentMark =""
                               ], 
                 Component[2]= [ 
                               componentIndex="", 
                               componentMark =""
                               ], 
                    }

global array

 var selectedComponentList = [];

 var selectElementList = [];

element

  $("#ElementTable").on("click", ".k1-grid-confirm", function () {

        var E_RecordId = $(this).data("id");
        var E_MarkingSchemeTitle = $("#" + E_RecordId + "_EMST").val();
        var E_AvailableMark = $("#" + E_RecordId + "_AM").val();
        var E_PassMark = $("#" + E_RecordId + "_PM").val();
        var E_MeritMark = $("#" + E_RecordId + "_MM").val();
        var E_DistinctionMark = $("#" + E_RecordId + "_DM").val();

        alert("elementRecordId " + E_RecordId + " E_MarkingSchemeTitle " + E_MarkingSchemeTitle + " E_AvailableMark " + E_AvailableMark + " E_PassMark " + E_PassMark + " E_MeritMark " + E_MeritMark + " E_DistinctionMark " + E_DistinctionMark);

        //add data to array//

        selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark });

        }
    });

Component

$("#ComponentSchemeTable").on("click", ".k-grid-confirm", function () {

        var recordId = $(this).data("id");
        var ComponentSchemeMark = $("#" + recordId + "_CM").val();
       //
        $(this).hide();

        $(this).siblings(".k-grid-input").hide();

        $(this).siblings(".k-grid-cancel").hide();

        $(this).siblings(".k-grid-Remove").show();

        //add data to array//
        selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });

         //push array to Selected Element

              ?????????????????????????????????????
        }
    });

Many thanks

Define a function to refresh the global list:

// Elements
selectElementList.push({...});
refreshGlobalList();

// Components
selectedComponentList.push({...});
refreshGlobalList();

And the function:

var globalList = [];
function refreshGlobalList() {
  globalList = selectElementList.concat(selectedComponentList);
}
arrayNum.push.apply(arrayNum, arrayValuTwo);
arrayNum.push.apply(arrayNum, arrayValuThree);

you can try this or you can try this

arrayNumber.push.apply(arrayNumber, arrayValueTwo.concat(arrayValueThree));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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