0

I have a array in data() :

data()  {
  return {
    list: [],
 }
},

methods: {
  pushData() {
     this.list.push({name:'yorn', age: 20});
  }
}

Now I want to push to the 'list' array of the following format, The key is info:

list [
     info [
     {
       name:yorn,
       age: 20
     }
  ]
 ]

I'm new to vuejs and javascript so I need everyone's help. Please give me your opinion. Thanks

1
  • I added an answer. Hope it will work as per your expectation. Commented Mar 12, 2022 at 16:15

4 Answers 4

1

I want to push to the 'list' array of the following format, The key is info

list: [ info: [ { name:yorn, age: 20 } ] ]

The above expected result is not a valid JSON. It should be like below :

list: [{
    info: [{
        name: yorn,
        age: 20
    }]
}]

Working Demo :

new Vue({
  el: '#app',
  data: {
    list: []
  },
  mounted() {
    this.pushData();
  },
  methods: {
    pushData() {
      this.list.push({info : [{name:'yorn', age: 20}] });
      // Or you can also use below one.
      // this.list[0].info.push({name:'yorn', age: 20});
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="(obj, index) in list" :key="index">
    <p v-for="(item, i) in obj.info" :key="i">{{ item.name }}       </p>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly the answer I expected. Thank you for your help with my work
1

Try altering the pushData method to have data parameter

pushData(data) {
 this.list.push(data);
}

Invoke the method

this.pushData({name: "john", age: 25});

2 Comments

Thanks @Nader. But I want an external info key, can you suggest me?
If I understand you correctly, you need smth like: ``` const info = [{name: "john", age: 25}]; this.pushData(info) ``` This will result in a list array with the first index being another array (info).
1

As I understand, you are trying to create a nested array. However, in an array you don't use a key but an index. What you are looking for is called an associative array.

Option a: Use the index of a 'normal' array

list = [{name: 'abc', age: 10},{name: 'def', age: 20}]

This way you can use the data in your array by using the index:

list[0] == {name: 'abc', age: 10}

list[1] == {name: 'def', age: 20}

Option b: Use an associative array

list = { info1: {name: 'abc', age: 10}, info2: {name: 'def', age: 20}}

This way you can use a key instead of an index. You just need different brackets for an associative array.

Hope this is helpful. :-)

Comments

1

You can simply use like below

pushData() {
 let data = { info : [{name:'yorn', age: 20}] }
 this.list.push(data);

}

Hope this is helpful. :-)

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.