Home > database >  calling an array method on a computed property
calling an array method on a computed property

Time:10-30

I have an array: const arr = []

I then have a computed property:

const filtered_arr = computed(() => {
 //...
}

I then have another computed property: filtered_arr2 where I want to call some() method on the first filtered_arr. But getting errors since filtered_arr is not an actual array.

const filtered_arr2 = computed(() => {
  filtered_arr.some((e)=>{}) // <- error
}

UPDATED:

Getting several errors :

  1. some is not a function

  2. Container is not set or can not be properly recognized. Use container() method to set it

I think my whole approach is buggy...

CodePudding user response:

From the API documentation...

computed()#

Takes a getter function and returns a readonly reactive ref object

and

ref()#

Takes an inner value and returns a reactive and mutable ref object, which has a single property .value that points to the inner value

So with that in mind, you'll need to use the .value property within your setup

const filtered_arr = computed(() => {
  // return some array
});

const filtered_arr2 = computed(() => {
  const isPresent = filtered_arr.value.some(...);
  // return some other value
});
  • Related