簡體   English   中英

如何在 JavaScript 中的嵌套 Arrays 中插入鍵值對

[英]How to Insert Key Value Pairs inside Nested Arrays in JavaScript

我需要實現相同的 output 但如您所見,ID 數組的長度為零,因為我無法使用push命令實現此 output,它會生成如下錯誤:

push is not a function

Cannot use indexOf for undefined or false

我需要使用推送命令解決此數組並使 output 與下面完全相同,但我不能使用each function 因為長度為零。

var RepeaterClass = {
    Repeaters: [],
    collectRepeaterValues: function (rep_id, cat_id, element_id) {
        this.Repeaters[rep_id] = this.Repeaters[rep_id] || [];
        this.Repeaters[rep_id][cat_id] = this.Repeaters[rep_id][cat_id] || [];

        if (-1 === this.Repeaters[rep_id][cat_id].indexOf(element_id)) {
            this.Repeaters[rep_id][cat_id].push(element_id);
        }
    },
};

這段代碼的實現:

ID_1: Array(0)
   category: Array(1)
      0: "dog"
      
   animals: Array(2)
      0: "dog"
      1: "cat"

正如其他人所評論的那樣,您在這里要問什么並不完全清楚。 如果您修復 var Repeaters =[]; 行,則此代碼可以正常工作

我認為混淆是因為我們將Repeaters創建為一個數組,但是我認為您必須使用rep_id和cat_id的字符串(例如'ID_1'和'animals')調用collectRepeaterValues才能獲得您正在展示的output。 如果要創建 arrays,則應使用數字調用它。 您不能使用字符串訪問數組元素。

如果您使用字符串調用 JavaScript 將在您執行時在數組上創建 object 屬性Repeaters[rep_id] = Repeaters[rep_id] || [] Repeaters[rep_id] = Repeaters[rep_id] || [] 也就是說,如果我們執行JavaScript中的語句Repeaters['ID_1'] = [] ,即使Repeaters是一個數組,它也不會進行數組賦值。 它將創建一個名為 ID_1 的 object 屬性並將其值設為空數組。

下面的代碼片段顯示了使用數字和字符串以及結果調用(更正后的)object。

順便說一句,collectRepeaterValues 中的 if 語句不起作用。

現在我們回到真正的問題上來。 您想要 arrays,當然必須按數字索引,還是想要具有字符串屬性的對象?

 // CALLING WITH STRINGS var RepeaterClass = { Repeaters: [], // Fixed so it's an object property collectRepeaterValues: function (rep_id, cat_id, element_id) { this.Repeaters[rep_id] = this.Repeaters[rep_id] || []; this.Repeaters[rep_id][cat_id] = this.Repeaters[rep_id][cat_id] || []; if (-1 === this.Repeaters[rep_id][cat_id].indexOf(element_id)) { this.Repeaters[rep_id][cat_id].push(element_id); } } } // What I think you're doing? RepeaterClass.collectRepeaterValues("ID_1", "category", "dog"); RepeaterClass.collectRepeaterValues("ID_1", "animals", "dog"); RepeaterClass.collectRepeaterValues("ID_1", "animals", "cat"); // At the top level RepeaterClass.Repeaters is an empty array with a 'ID_1' property // Array length is zero... console.log(RepeaterClass.Repeaters.length); // 0 // But we have a property ID_1, that is itself an array of zero length with category // and animals properties that are arrays console.log(RepeaterClass.Repeaters.ID_1.category[0]); // dog console.log(RepeaterClass.Repeaters.ID_1.animals[0]); // dog console.log(RepeaterClass.Repeaters.ID_1.animals[1]); // cat // Note that this IS the result at the end of the question // EDIT: You can iterate over the properties with for..in console.log('Iterating categories on ID_1:'); for (var cat_id in RepeaterClass.Repeaters.ID_1) { console.log(cat_id); }

 // CALLING WITH NUMBERS var RepeaterClass = { Repeaters: [], // Fixed so it's an object property collectRepeaterValues: function (rep_id, cat_id, element_id) { this.Repeaters[rep_id] = this.Repeaters[rep_id] || []; this.Repeaters[rep_id][cat_id] = this.Repeaters[rep_id][cat_id] || []; if (-1 === this.Repeaters[rep_id][cat_id].indexOf(element_id)) { this.Repeaters[rep_id][cat_id].push(element_id); } } } // How this code is meant to be called I think RepeaterClass.collectRepeaterValues(0, 0, "dog"); RepeaterClass.collectRepeaterValues(0, 1, "dog"); RepeaterClass.collectRepeaterValues(0, 1, "cat"); // At the top level RepeaterClass.Repeaters is now an array structure console.log(RepeaterClass.Repeaters.length); // 1 console.log(RepeaterClass.Repeaters[0][0][0]); // dog console.log(RepeaterClass.Repeaters[0][1][0]); // dog console.log(RepeaterClass.Repeaters[0][1][1]); // cat

我找到的唯一解決方案是創建另一個數組來存儲中繼器內的元素,然后將其推送到函數外部的主中繼器數組中

但仍然無法在同一個 function 中實現。

var RepeaterClass = {
    
    Repeaters: {},
    validated: {},

    collectRepeaterValues: function( cat_id, element_id ) {
            
        // this.Repeaters[ rep_id ]    = this.Repeaters[ rep_id ] || [];
        this.validated[ cat_id ] = this.validated[ cat_id ] || [];

        if ( -1 === this.validated[ cat_id ].indexOf( element_id ) ) {
            
            this.validated[ cat_id ].push( element_id );
                
        }

    }


    AnotherFunction: function() {

        _.each( REP, function( repDetails, repID ) {

            _.each( repDetails, function( value ) {
                /* 1. Call the Collector */
                collectRepeaterValues( value['cat'], value['id'] );

            } );

            /* 2. push the validated output inside the main array */
            this.Repeaters[ repID ] = this.Repeaters[ repID ] || [];
            this.Repeaters[ repID ] = this.validated;
            
            /* Empty for another session */
            this.validated = {};

        } );


    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM