简体   繁体   English

如何将对象中的 Javascript 嵌套对象更改为索引数组?

[英]How to change a Javascript nested object in object to an indexed array?

I'm fairly new to developing, so need some help with an object.我对开发还很陌生,所以需要一些关于对象的帮助。 I tried doing a for loop and pushing the values into an array.我尝试执行 for 循环并将值推送到数组中。 The problem is that it is pushing all the keys/values as one index rather than indexing each key/value separately.问题在于它将所有键/值作为一个索引推送,而不是单独索引每个键/值。 I am working with a key/value object as follows:我正在使用一个键/值对象,如下所示:

var obj = {
   "application": {
      "create": "false",
      "read": "true",
      "update": "true",
      "delete": "false"
   },
   "connection": {
      "create": "false",
      "read": "true",
      "update": "true",
      "delete": "false"
   }
}

I need each key(application and connection) to be indexed - the keys need to be values.我需要对每个键(应用程序和连接)进行索引 - 键必须是值。 For example the following would retrieve:例如,以下将检索:

  obj[0] = application
  obj[0].create = false
  obj[1] = connection

Thanks for your help.谢谢你的帮助。

You can add an extra field key to store the keys of the object obj .您可以添加一个额外的字段key来存储对象obj的键。 This is produce an array of objects.这是产生一个对象array

 var obj = { "application": { "create": "false", "read": "true", "update": "true", "delete": "false" }, "connection": { "create": "false", "read": "true", "update": "true", "delete": "false" } } var keys = Object.keys(obj) var arr = [] keys.map(function(key, i) { arr[i] = obj[key] arr[i].key = key }) console.log(arr[0].key) console.log(arr[0].create) console.log(arr[1].key)

If you want each property of the object pushing into an array, you can iterate over the object contents like so:如果您希望将对象的每个属性推入一个数组,您可以像这样迭代对象内容:

for (var item in obj){
    /* item will equal the first property of obj, in this case: 'application' */
    array.push(item)
}

array = ["application", "connection"]

You can use the same technique on each object property, to index their children into the array.您可以对每个对象属性使用相同的技术,将其子项索引到数组中。

By the outcome you expect, it looks like you want to transform the first level of your object to array cells.根据您预期的结果,您似乎想将对象的第一级转换为数组单元格。

This will do it:这将做到:

var arr = [], i=0;
for (var key in obj) {
   arr[key] = obj[key] // For accessing via the property name for example e.g. application
   arr[i] = obj[key] // For index access for example obj[0]
   i++;
}

You can also use the .map method to do it.您也可以使用 .map 方法来做到这一点。

You could iterate over the keys and assamble a new array with the children of the inner object.您可以遍历键并与内部对象的子对象组合一个新数组。

 var obj = { application: { create: "false", read: "true", update: "true", delete: "false" }, connection: { create: "false", read: "true", update: "true", delete: "false" } }, array = Object.keys(obj).map(function (k) { return [k].concat(Object.keys(obj[k]).map(function (kk) { return obj[k][kk]; })); }); console.log(array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM