如何循环遍历一个数组并以对象形式输出结果

huangapple go评论75阅读模式
英文:

How to loop through an array and get output in object

问题

我正在从我的数据库获取数据,并希望按需循环。

我从我的数据库获取到对象数组,我希望将其转换为带有一些更改的对象。

我的数据:

Data=   [
  {
    "Menu": "Menu1",
    "SubMenu": "dash",
    "PATH": "Path1"
  },
  {
    "Menu": "Menu2",
    "SubMenu": "SubMenu1",
    "PATH": "path2"
  },
  {
    "menu": "menu2",
    "SubMenu": "SubMenu2",
    "PATH": "path3"
  },
  {
    "Menu": "menu2",
    "Submenu": "SubMenu3",
    "PATH": "Path4"
  }
]

我想要将其转换成以下格式:

{
  "menu1": [
    {
      "SubMenu": "dash",
      "PATH": "path1"
    }
  ],
  "menu2": [
    {
      "SubMenu": "subMenu1",
      "PATH": "Path2"
    },
    {
      "SubMenu": "Submenu2",
      "PATH": "Path3"
    },
    {
      "type": "SubMenu3",
      "link": "Path4"
    }
  ]
}
const newArray= Data.map(element => element.Menu);
console.log(newArray);

希望这些信息对你有所帮助。

英文:

I am getting Data from my db And I want to loop as required

I am getting array of objects from my data base and I want to convert it to objects with some changes

My Data

 Data=   [
  {
    "Menu": "Menu1",
    "SubMenu": "dash",
    "PATH": "Path1"
  },
  {
    "Menu": "Menu2",
    "SubMenu": "SubMenu1",
    "PATH": "path2"
  },
  {
    "menu": "menu2",
    "SubMenu": "SubMenu2",
    "PATH": "path3"
  },
  {
    "Menu": "menu2",
    "Submenu": "SubMenu3",
    "PATH": "Path4"
  }
]

I want to convert it into like below -:

    {
  "menu1": [
    {
      "SubMenu": "dash",
      "PATH": "path1"
    }
  ],
  "menu2": [
    {
      "SubMenu": "subMenu1",
      "PATH": "Path2"
    },
    {
      "SubMenu": "Submenu2",
      "PATH": "Path3"
    },
    {
      "type": "SubMenu3",
      "link": "Path4"
    }
  ]
}


   

const newArray= Data.map(element => element.Menu);
console.log(newArray);

答案1

得分: 2

你可以使用 Array.reduce()

var Data = [{
    "Menu": "Menu1",
    "SubMenu": "dash",
    "PATH": "Path1"
  },
  {
    "Menu": "Menu2",
    "SubMenu": "SubMenu1",
    "PATH": "path2"
  },
  {
    "Menu": "Menu2",
    "SubMenu": "SubMenu2",
    "PATH": "path3"
  },
  {
    "Menu": "Menu2",
    "SubMenu": "SubMenu3",
    "PATH": "Path4"
  }
];

var res = Data.reduce((acc, item) => {
  var accObj = {
    Submenu: item.SubMenu,
    PATH: item.PATH
  };
  if (acc[item.Menu]) {
    acc[item.Menu].push(accObj);
  } else {
    acc[item.Menu] = [accObj];
  }
  return acc;
}, {});

console.log(res);
英文:

You can use Array.reduce():

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var Data = [{
    &quot;Menu&quot;: &quot;Menu1&quot;,
    &quot;SubMenu&quot;: &quot;dash&quot;,
    &quot;PATH&quot;: &quot;Path1&quot;
  },
  {
    &quot;Menu&quot;: &quot;Menu2&quot;,
    &quot;SubMenu&quot;: &quot;SubMenu1&quot;,
    &quot;PATH&quot;: &quot;path2&quot;
  },
  {
    &quot;Menu&quot;: &quot;Menu2&quot;,
    &quot;SubMenu&quot;: &quot;SubMenu2&quot;,
    &quot;PATH&quot;: &quot;path3&quot;
  },
  {
    &quot;Menu&quot;: &quot;Menu2&quot;,
    &quot;SubMenu&quot;: &quot;SubMenu3&quot;,
    &quot;PATH&quot;: &quot;Path4&quot;
  }
];

var res = Data.reduce((acc, item) =&gt; {
  var accObj = {
    Submenu: item.SubMenu,
    PATH: item.PATH
  };
  if(acc[item.Menu]) {
    acc[item.Menu].push(accObj);
  } else {
    acc[item.Menu] = [accObj];
  }
  return acc;
}, {});

console.log(res);

<!-- end snippet -->

答案2

得分: 0

你使用 map 是正确的方向,但在迭代之前应该初始化一个对象(result = {}),然后随着迭代的进行逐步填充它。然后,你将不再使用 .map,而应该使用 .forEach,因为你不会使用输出。

const result = {}
Data.forEach(item => result[item.Menu] = {
    //...
})
英文:

You're in the right direction with map, however you should initialise an object before you iterate (result = {}) and populate it as you go. You'd then not use .map but .forEach because you won't be using the output.

const result = {}
Data.forEach(item =&gt; result[item.Menu] = {
    //...
})

</details>



# 答案3
**得分**: 0

输出结果:

```json
{
	Menu1: [{
		SubMenu: 'dash',
		PATH: 'Path1'
	}],
	Menu2: [{
			SubMenu: 'SubMenu1',
			PATH: 'path2'
		},
		{
			SubMenu: 'SubMenu2',
			PATH: 'path3'
		},
		{
			Submenu: 'SubMenu3',
			PATH: 'Path4'
		}
	]
}
英文:

Try for this:

var obj = {};
  Data.forEach(function(element){
    var menu = element[&quot;Menu&quot;];
    delete element[&quot;Menu&quot;];
    if(Object.keys(obj).includes(menu)){            
        obj[menu].push(element)
    } else {            
        obj[menu] = [element]
    }
  })   
  console.log(&quot;oo&quot;, obj);

OUTPUT:

{
	Menu1: [{
		SubMenu: &#39;dash&#39;,
		PATH: &#39;Path1&#39;
	}],
	Menu2: [{
			SubMenu: &#39;SubMenu1&#39;,
			PATH: &#39;path2&#39;
		},
		{
			SubMenu: &#39;SubMenu2&#39;,
			PATH: &#39;path3&#39;
		},
		{
			Submenu: &#39;SubMenu3&#39;,
			PATH: &#39;Path4&#39;
		}
	]
}

huangapple
  • 本文由 发表于 2020年1月3日 18:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定