【问题标题】:How to compare json elements and display data in multidimensional array if they match?如果匹配,如何比较json元素并在多维数组中显示数据?
【发布时间】:2016-02-02 13:00:04
【问题描述】:

我有一个 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] 包含“字段”的每个对象迭代,并且仅在控制台中显示最后一个对象。

我想知道如何在“字段”和“表”之间进行比较条件,以将每次迭代都放在单独的数组中。

这是一个证明问题的FIDDLE(抱歉,如果我无法清楚地表达我的解释)。

【问题讨论】:

  • 既然你使用OFImport作为对象,你也应该将它初始化为对象:OFImport = {}(不是OFImport = []

标签: javascript jquery arrays json multidimensional-array


【解决方案1】:

如果我了解您想要做什么,即拥有一组表,其中包含一组字段,那么我认为您的 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);

输出:-

【讨论】:

  • 确实!非常感谢您的帮助!
  • 请问使用这个解决方案是否可以获得与我的Fiddle 相同的输出?我不知道如何用selectedObject 重命名objectArray 并将fieldtype 上移一级。
  • 我已更新我的答案以设置名称。但是你为什么要让fieldtype 更上一层楼呢?如果它们在表格属性的数组之外,那么它们每次都会覆盖。
  • 那是我最初的问题,它会帮助我处理我的其余代码。无论如何,我感谢您的支持,并再次感谢您对我的帮助。
猜你喜欢
  • 2017-03-25
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
相关资源
最近更新 更多