簡體   English   中英

如果它們匹配,如何比較json元素並在多維數組中顯示數據?

[英]How to compare json elements and display data in multidimensional array if they match?

我可以使用一個JSON對象,如下所示:

{
      "Fields": [
    {
      "table": "catalogue",
      "field": "Histo_Qtite",
      "type": "STRING"
    },
    {
      "table": "catalogue",
      "field": "id_article",
      "type": "STRING"
    },
    {
      "table": "contact",
      "field": "contact_email",
      "type": "STRING"
    },
    {
      "table": "contact",
      "field": "contact_firestname",
      "type": "STRING"
    },
    {
      "table": "customer",
      "field": "activity_type",
      "type": "STRING"
    },
    {
      "table": "customer",
      "field": "adress",
      "type": "STRING"
    }
  ],
  "Tables": [
    {
      "entity": "CATALOGUE",
      "table": "catalogue"
    },
    {
      "entity": "CLIENT",
      "table": "customer"
    },
    {
      "entity": "CONTACT",
      "table": "contact"
    }
  ]
}

我試圖基於表的名稱為每個“字段”對象創建一個多維數組。 為此,我嘗試了javascript,並生成了以下代碼:

var objectPREFIX = "object_",
selectedObject = '',
objectArray = [],
objectImport = [],
OFImport = [],
TablesLength = jsonImport.Tables.length,
FieldsLength = jsonImport.Fields.length;

 for (i = 0; i < FieldsLength; i++) {

    selectedObject = objectPREFIX + jsonImport.Fields[i].table;

    OFImport[selectedObject] = {
        tableName : jsonImport.Fields[i].table,
        FieldName : jsonImport.Fields[i].field,
        fieldType : jsonImport.Fields[i].type
    }

    for (j = 0; j < TablesLength; j++) {

        if(OFImport[selectedObject].tableName == jsonImport.Tables[j].table) {

            objectImport.push(OFImport[selectedObject]);
            objectArray[selectedObject] = OFImport[selectedObject];
        }
    }
}

console.log(objectArray);

據我了解,問題是OFImport[selectedObject]包含“字段”的每個對象迭代,並且僅在控制台中顯示最后一個對象。

我想知道如何在“字段”和“表”之間進行比較,以使每個迭代在單獨的數組中進行。

這是一個說明問題的字段 (對不起,如果我在解釋時遇到麻煩)。

如果我了解您要做什么,那就是有一個表數組,有一個字段數組,那么我認為您的for循環會倒退。

您需要先循環表,然后像這樣添加字段:-

 jsonImport = { "Fields": [{ "table": "catalogue", "field": "Histo_Qtite", "type": "STRING" }, { "table": "catalogue", "field": "id_article", "type": "STRING" }, { "table": "contact", "field": "contact_email", "type": "STRING" }, { "table": "contact", "field": "contact_firstname", "type": "STRING" }, { "table": "customer", "field": "activity_type", "type": "STRING" }, { "table": "customer", "field": "adress", "type": "STRING" }], "Tables": [{ "entity": "CATALOGUE", "table": "catalogue" }, { "entity": "CLIENT", "table": "customer" }, { "entity": "CONTACT", "table": "contact" }] } var objectArray = [], objectPREFIX = "object_", selectedObject = '', TablesLength = jsonImport.Tables.length, FieldsLength = jsonImport.Fields.length; for (i = 0; i < TablesLength; i++) { selectedObject = objectPREFIX + jsonImport.Tables[i].table; objectArray[selectedObject] = { table: jsonImport.Tables[i].table, entity: jsonImport.Tables[i].entity, Fields: [] } for (j = 0; j < FieldsLength; j++) { if (jsonImport.Tables[i].table == jsonImport.Fields[j].table) { objectArray[selectedObject].Fields.push({ "field": jsonImport.Fields[j].field, "type": jsonImport.Fields[j].type }); } } } console.log(objectArray); 

輸出: -

在此處輸入圖片說明

暫無
暫無

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

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