简体   繁体   中英

Make object properties by looping through array - JS

I am working with Vue and I got this prop that's actually an array. In this array I can have multiple kind of items. For each item, I would like to make a property for a specific object. So If I have an array with methods and count , I would like to have an object like

   const data = {
       name: newObject.name,
       methods: newObject.methods,
       count: newObject.count,
       // All other properties here
   }; 

JS I've got

this.inputs.forEach((input) => {
   // Make property here
});

            // Create properties here maybe?
            // newObject.methods ?

    // Set a data object for post rquest
   const data = {
       name: newObject.name,
       // All other properties here
   };

How can I make this happen? I thought of an forEach , but after that I just don't know what to do.

prop

`:inputs="['Methods']"`

And then loop throught that inputs prop to get for each item a property on that newObject object

If I understand you correctly, you are trying to parse a property into a component, then loop through this property and assign it to the local data storage using a forEach loop,

Here is a example Fiddle: https://jsfiddle.net/eywraw8t/70166/

Vue.component('child', {
  props: ['data'],
  data: function() {
    return {
        'newItems': [],
    }
  },
  mounted: function(){
    this.data.forEach((input) => {
        this.newItems.push(input.data)
    });


    console.log(this.newItems)
  },
  template: `<div>{{ newItems }}</div>`
});

new Vue({
  el: "#app",
  data() {
    return {
      data: [
          { 'data': 123, 'other': 678 },
          { 'data': 233, 'other': 6728 },
          { 'data': 343, 'other': 67812 },
        ]
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM