How to remove “[” and “]” signs from array in Javascript
Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How to remove “[” and “]” signs from array in Javascript on this date .
I’m using react.js
for building my dashboard
I want to convert an array like this (old version) [ {...}, {...}, {...} ]
into this (new version) {...}, {...}, {...}
in javascript
So I can put the new version of the array inside a JSON array like this [ {...}, newArray ]
I know a map
function returns an array and I know it’s a silly question but I wonder how
here is my code:
const siteProfilesList = ['ABC', 'DEF', 'GHI'] const pagesList = ['Dashboard', 'Routes', 'Payload'] const siteProfileNavigationsList = siteProfilesList.map((item, index) => { let menu = {} menu['_tag'] = 'CSidebarNavDropdown' menu['name'] = item menu['_children'] = pagesList.map((pageItem, pageIndex) => { let pageMenu = {} pageMenu['_tag'] = 'CSidebarNavItem' pageMenu['name'] = pageItem pageMenu['to'] = `/${pageItem.toLowerCase()}/location=${item.toLowerCase()}` return pageMenu }) return menu }) const navigations = [ { _tag: 'CSidebarNavTitle', _children: ['Site Profile'] }, siteProfileNavigationsList ] export default navigations
I know it’s a silly question but I just wonder about the solution.
Answer
Is that what you want? I use flat()
.
const siteProfilesList = ["ABC", "DEF", "GHI"]; const pagesList = ["Dashboard", "Routes", "Payload"]; const siteProfileNavigationsList = siteProfilesList.map((item, index) => { let menu = {}; menu["_tag"] = "CSidebarNavDropdown"; menu["name"] = item; menu["_children"] = pagesList.map((pageItem, pageIndex) => { let pageMenu = {}; pageMenu["_tag"] = "CSidebarNavItem"; pageMenu["name"] = pageItem; pageMenu[ "to" ] = `/${pageItem.toLowerCase()}/location=${item.toLowerCase()}`; return pageMenu; }); return menu; }); const navigations = [ { _tag: "CSidebarNavTitle", _children: ["Site Profile"], }, siteProfileNavigationsList, ]; console.log(navigations.flat());