0

I have an object in my Vue instance with the name of items

<script>
export default {
  data() {
    return {
      selected: "",
      items: {
        item1: [{ selected: "", inputType: "", inputTarget: "" }],
        item2: [{ selected: "", inputType: "", inputTarget: "" }]
      },
      textarea: ""
    };
  },
  methods: {
    selectboxAction(index) {
      this.items.item1.forEach(val => {
        if (val.selected.toLowerCase() === "file") {
          this.$refs.inputData[index].type = "file";
        } else {
          this.$refs.inputData[index].type = "text";
        }
      });
    }
  }
};
</script>

how can I fetch an array of it, I want to put some condition over every item, but it could have item more than 2 items, maybe in the future it reaches 100

as you can see in selectboxAction method, I was able to fetch only one item which is item1 in this case

how can I fetch all of the arrays from items, not just one item1

2

3 Answers 3

2

I suggest you format your data using a computed getter and use Object.keys as others have suggested.

get itemsArray() {
  return Object.keys(this.items).map((key) =>
    return this.items[key];
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of naming your 'items' properties 'item1', 'item2', etc, it would be better make 'items' an array and add an 'id' property to each 'items' object:

  data() {
    return {
      selected: "",
      items: [
        { id: 1, selected: "", inputType: "", inputTarget: "" },
        { id: 2, selected: "", inputType: "", inputTarget: "" }
      ],
      textarea: ""
    };
  },

Comments

1

you can do something like

 methods: {
    selectboxAction(index) {
      Object.keys(this.items).forEach(val => {
        this.items[val].forEach(item => {
        if (item.selected.toLowerCase() === "file") {
          this.$refs.inputData[index].type = "file";
        } else {
          this.$refs.inputData[index].type = "text";
        }
       });
      });
    }
  }

Comments

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.