0

I have a function in a javascript file where I return an array. But when I call this function, when I look at the type with the "typeof" command, it returns an object instead of an array.

My javascript file is here.

import {useStore} from "vuex";
import {computed} from "vue";

export const getActions = (menuId) => {
   const store = useStore()
    const loginInfo = computed(() => {
        return store.state.Identity.loginInfo
    });
    const actions = []
    loginInfo.value.Authorization.forEach((x)=>{
        let splitData = x.Id.split('-')
        if(splitData[0] === '02' && splitData[1] === menuId){
            if(!actions.some(item => item.Id === splitData[2]))
                actions.push({
                    Id:splitData[2],
                    Definition: x.Definition,
                    Clicked:false
                })
        }
    })
    return actions;
}

Here is where I call and use this function.

 let actions =[]
    actions =  getActions(props.menuId)
      for(let i=0; actions.length;i++){
        if(props.actionId === actions[i].Id)
          return isAuth.value = false
        else
          isAuth.value = true
      }

Although my variable named actions is an array, it sees it as an object and my computer starts freezing. My computer's fan starts running very fast and chrome starts to freeze.

6
  • typeof [] -> "object". Have you read e.g. stackoverflow.com/q/12996871/3001761? It's unclear how if at all that's related to the performance issue. Commented May 29, 2022 at 15:58
  • yes, I read it and I think it doesn't have an exact equivalent in javascript. But my problem is that when I call this function, even though I return the function as an array, why does it return an object object to me. @jonrsharpe Commented May 29, 2022 at 16:02
  • 2
    An array is an object. It's unclear how you think that relates to your actual problem. Commented May 29, 2022 at 16:03
  • Your for loop is going to run forever, which might be why your browser is freezing and consuming all available CPU; the loop never yields. Also, based on your usage of actions, you almost certainly want a Map and not an array. Commented May 29, 2022 at 16:04
  • So how can I browse the array without using a for loop?@Dan Commented May 29, 2022 at 16:05

1 Answer 1

2

You didn't set your loop right:

for(let i = 0; i < actions.length; i++){
Sign up to request clarification or add additional context in comments.

3 Comments

how so? I already did as you wrote above. What is wrong?
Your code sample does not include i <. The second term in the loop is just actions.length which will always be truthy as long as there are items in the array.
@Celal Poyraz you are welcome mate, cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.