1

I am working on nuxt 3 (vue 3) and I need to watch the particular item of the specific object from the array.

My array

const formData = reactive({
   addressDetails: [
     {
      address: "",
      street: "",
      suburb: "",
      state: "",
      postCode: "",
      country: "",
      unitNumber: "",
      streetNumber: "",
      streetName: "",
      timeAtaddressYears: "",
      timeAtaddressMonths: "0",
    },
     {
      address: "",
      street: "",
      suburb: "",
      state: "",
      postCode: "",
      country: "",
      unitNumber: "",
      streetNumber: "",
      streetName: "",
      timeAtaddressYears: "",
      timeAtaddressMonths: "0",
    },
   {
      address: "",
      street: "",
      suburb: "",
      state: "",
      postCode: "",
      country: "",
      unitNumber: "",
      streetNumber: "",
      streetName: "",
      timeAtaddressYears: "",
      timeAtaddressMonths: "0",
    },

   ]      
});

Here, I want to watch if any timeAtaddressYears items value has changed but not on the whole formData.addressDetails value. Ex if I add new address details then no need to any watch but in specific address details if I change any address's timeAtaddressYears then watch should be called.

3 Answers 3

2
const formData = reactive({
   addressDetails: [
     {
      address: "",
      street: "",
      suburb: "",
      state: "",
      postCode: "",
      country: "",
      unitNumber: "",
      streetNumber: "",
      streetName: "",
      timeAtaddressYears: "",
      timeAtaddressMonths: "0",
    },
   ]      
})

const selected = computed(() => {
  return formData.addressDetails.map(form => form.timeAtaddressYears)
})

watch(selected, (newValue, oldValue) => {
      console.log(newValue, oldValue)
})
Sign up to request clarification or add additional context in comments.

4 Comments

As I mentioned I am working with nuxt 3 and nuxt 3 is based on Vue 3 so this syntax will not work and I need an extra level watch as I mentioned. Thanks for your comment
@KishanBhensadadiya Now I added code with VUE3 syntax and you have to do some modification with your logic and It will be help for you. Thanks
Thank you for your revert try! I think it is a lengthy process using computed and watch both to indicate only one change. I have posted the answer on my way as per the Vue 3 documentation
Okay, My code is working for only change value of timeAtaddressYears from your array of object.
0

You can try this:

watch(() => formData.addressDetails[0].address, (newValue, oldValue) => {

});

4 Comments

Thanks for your comment but I also need to watch if formData.addressDetails[1].address value has changed.
if(formData.addressDetails[1].address !== previousAddressValue) { console.log("The address valuechanged."); } try this may be works
It will not work. I think you didn't clear with requirements. Please check the whole thing first and address details are dynamic and it is not fixed user can add or remove any address
@KishanBhensadadiya You should add more array entries in your original post, so people understand that you want an abstract solution.
0

I have found a similar one, we need to add a deep watcher if we want to watch the particular item of the specific object from the array.

VueJS Deepwatchers

watch(
  () => formData,
  (newValue, oldValue) => {
    console.log(newValue, oldValue);
  },
  { deep: true });

1 Comment

As per I know we can also watch formData.addressDetails only because if formData contains other state than addressDetails it will also watch for those too.

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.