繁体   English   中英

通过遍历数组来创建对象属性-JS

[英]Make object properties by looping through array - JS

我正在与Vue合作,我得到了这个道具,它实际上是一个数组。 在这个数组中,我可以有多种项目。 对于每个项目,我都想为特定对象创建一个属性。 所以,如果我有一个带有methodscount的数组,我想有一个像

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

JS我有

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
   };

我怎样才能做到这一点? 我想到了forEach ,但是之后我不知道该怎么办。

支柱

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

然后遍历inputs prop的遍历,以获取每个项目在该newObject对象上的属性

如果我对您的理解正确,则您尝试将属性解析为组件,然后遍历该属性,并使用forEach循环将其分配给本地数据存储,

这是一个小提琴的例子: 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 },
        ]
    }
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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