Home > database >  how should javascript array control be
how should javascript array control be

Time:04-25

there is an array data as below. I will have a nav. operation on the incoming data data in the array, otherwise I will print the data it is, I wonder how can I do this?`

title: item.title,

if item.title in the incoming data nav. If there is Let this.$t(item.title) pass the print check. but if there is no nav. then check if this.$t(item.title)

menu_items: [
        {
          title: 'nav.home',
          path: '/',
          Index: 0
        },
        {
          title: 'nav.gallery',
          path: '/gallery',
          order: 10
        },
        {
          title: 'nav.contact',
          path: '/contact',
          order: 12
        },
        {
          title: 'hello',
          path: '/hello',
          order: 13
        },
        {
          title: 'hello2',
          path: '/hello2',
          order: 14
        }
      ]
menuItem() {
      return this.menu_items.map(item => {
        return {
          title: item.title,
          path: item.path,
          children: item.children,
          order: item.order
        }
      }).sort((a, b) => a.order - b.order)
    }

CodePudding user response:

first thing you need to do is search item.title item.title.search('nav.') If there is no nav. in the data after checking, if item.title exists, this.$t(item.title)

menuItem() {
      return this.menu_items.map(item => {
        return {
          title: item.title.search('nav.') ? item.title : this.$t(item.title),
          path: item.path,
          children: item.children,
          order: item.order
        }
      }).sort((a, b) => a.order - b.order)
    }
  • Related