JSON获取父级值到下拉列表

时间:2018-02-04 09:36:13

标签: javascript jquery html json

我需要将JSON文件的父值获取到Dropdown。以下是我的JSON。

[
{
    "AAA": {
        "task": {
            "taskname": "Promobox",
            "component": [["asd", "3"], ["asd", "3"]]
        },
        "task": {
            "taskname": "Newswire",
            "component": [["asd", "3"], ["asd", "3"]]
        },
    },

    "BBB": {
        "task": {
            "taskname": "Video",
            "component": [["asd", "3"], ["asd", "3"]]
        },
        "task": {
            "taskname": "Documents",
            "component": [["asd", "3"], ["asd", "3"]]
        },
    }
}
]

如果有值,我可以获取父元素(例如:" AAA":" text")但在这种情况下,父项没有值。以下是我的完整代码。我需要的是在下拉列表中获得AAA,BBB。

JS FIDDDLE

5 个答案:

答案 0 :(得分:0)

迭代对象数组并使用for..in获取密钥

var arr = [{
  "AAA": {
    "task": {
      "taskname": "Promobox",
      "component": [
        ["asd", "3"],
        ["asd", "3"]
      ]
    },
    "task": {
      "taskname": "Newswire",
      "component": [
        ["asd", "3"],
        ["asd", "3"]
      ]
    },
  },

  "BBB": {
    "task": {
      "taskname": "Video",
      "component": [
        ["asd", "3"],
        ["asd", "3"]
      ]
    },
    "task": {
      "taskname": "Documents",
      "component": [
        ["asd", "3"],
        ["asd", "3"]
      ]
    },
  }
}]

var getKeys = [];
arr.forEach(function(item) {
  for (var keys in item) {
    getKeys.push(keys)
  }

})

console.log(getKeys)

答案 1 :(得分:0)

数组results只有一个元素results=[{}]。因此,您应将其索引为result[0]以获取内部对象。

在代码中,您将值赋给选项的value属性。但AAABBB实际上是indexes。他们有价值观。

工作代码是:



$(document).ready(function() {
  var $el = $("#dropDown");
  var results = [{
    "AAA": {
      "task": {
        "taskname": "Promobox",
        "component": [
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"]
        ]
      },
      "task": {
        "taskname": "Newswire",
        "component": [
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"]
        ]
      },
    },

    "BBB": {
      "task": {
        "taskname": "Video",
        "component": [
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"]
        ]
      },
      "task": {
        "taskname": "Documents",
        "component": [
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"],
          ["asd", "3"]
        ]
      },
    }
  }];
  $el.empty(); // remove old options
  $el.append($("<option></option>")
    .attr("value", '').text('Please Select'));
  $.each(results[0], function(index, value) {//result[0]
    $el.append($("<option></option>")
      .attr("value", index).text(index));//index
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropDown">
</select>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

现在,您在下拉列表中添加整个对象,您需要遍历对象以获取密钥。在你的函数内部将代码改为:

for (var key in value) {
   $el.append($("<option></option>")
         .attr("value", key).text(key));
}

https://jsfiddle.net/o5ju4kja/11/

答案 3 :(得分:0)

如何使用Object.keys?

var options = Object.keys(results[0]);

这将使选项成为&#34; AAA&#34;,&#34; BBB&#34;的数组。 循环槽选项并附加选项元素

答案 4 :(得分:0)

如果您只需要密钥,首先需要从数组中提取对象,这样您就可以var object = result[0]。他们只需执行var keys = Object.keys(object),就会获得['AAA', 'BBB', ...]

的数组