在另一个数组中添加javaScript数组

add javaScript array in another array

本文关键字:数组 javaScript 添加 另一个      更新时间:2023-09-26

我得到了javaScript数组中包含元素的记录,其中有唯一的elementIndex,现在我必须在同一个javaScript阵列中为该特定元素添加单个或多个组件(相同的elementIdex(。

该阵列可以具有所需的任意多个元件,并且每个元件可以具有与该元件相关联的一个或多个组件。

我已经完成了第一部分,我如何完成第二部分。。。即添加与单个元素关联的组件记录。

注意元素和组件在单独的javaScript函数中,但我有全局数组

这就是我想要实现的(可能是JSON(

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

全局数组

 var selectedComponentList = [];
 var selectElementList = [];

元素

  $("#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 });
        }
    });

组件

$("#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
              ?????????????????????????????????????
        }
    });

非常感谢

定义一个刷新全局列表的函数:

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

和功能:

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

你可以试试这个,也可以试试这个

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