简体   繁体   English

如何将新属性添加到数组的每个 object

[英]How to add new properties into every object of an array

How can I add new 2 properties inside the array of objects?如何在对象数组中添加新的 2 个属性? Those 2 properties should be added for every object inside the array.应该为阵列内的每个 object 添加这 2 个属性。 Here is the function:这是 function:

selectTag(selectedProduct, selectedTag) {
      this.selectedProducts.filter(item => {
        item.id === selectedProduct.id
      })
      .map(item => {
        item.tagId=selectedTag.id, item.tagTitle = selectedTag.title
        })
    },

dropdown落下

  <b-dropdown aria-role="list">
                <b-button
                  icon-right="caret-down"
                  class="ToolbarButton"
                  size="is-small"
                >
                  <span> {{ selectedProduct.tagTitle }} </span>
                </b-button>

                <b-dropdownitem
                  v-for="selectedTag in selectedProduct.tags"
                  :key="selectedTag.id"
                  aria-role="listitem"
                  @click="selectTag(selectedProduct, selectedTag)"
                >
                  {{ selectedTag.title }}
                </b-dropdownItem>

I tried above function but it didn't work.我在 function 上面试过,但没有用。 map method should be fixed. map 方法应该是固定的。 I am trying to add tagId and tagTitle properties which will get value from drop down selection for every product row... How can be it fixed?我正在尝试添加 tagId 和 tagTitle 属性,这些属性将从每个产品行的下拉选择中获取值...如何修复?

The map function indeed is wrong, you don't return anything, it should be like this: map function 确实是错的,你什么都不回,应该是这样的:

.map(item => ({
     ...item,
     tagId: selectedTag.id,
     tagTitle: selectedTag.title
}))

or或者

.map(item => {
  return {
    ...item,
    tagId: selectedTag.id,
    tagTitle: selectedTag.title
  }
})

you can loop on the object and add your properties:您可以在 object 上循环并添加您的属性:

for(let obj of array) {
    obj[key1] = value1;
    obj[key2] = value2;
}

Use .map with object spread syntax.使用.map和 object 扩展语法。

.map(item => ({
    ...item, 
    item.tagId: selectedTag.id, item.tagTitle: selectedTag.title
}))

Try this尝试这个

 var arr =[ {id: 1, turnName: "10_00_am"}, {id: 2, turnName: "10_00_am"}, {id: 3, turnName: "11_00_am"} ]; const addProps = (x) => { x.prop1 = 'Alaska'; x.prop2 = 'Canada'; return x; } var newArr = arr.map(x => addProps(x)); console.log(newArr);

You can use ES6 object spread syntax to return a new updated item from map function.您可以使用 ES6 object 扩展语法从 map function 返回一个新的更新项。 Also, the object properties can be destructured in the function parameters itself to make it look concise and neat.此外,object 属性可以在 function 参数本身中进行解构,使其看起来简洁明了。

selectTag(({id}), ({id: tagId, title: tagTitle})) {
    this.selectedProducts.filter(item => item.id === id)
    .map(item => ({...item, tagId, tagTitle}))
}

From the function you show, point out that the filter needs to store its result in some variable, since filter does not mutate the array in which it is executed.从您展示的 function 中,指出过滤器需要将其结果存储在某个变量中,因为过滤器不会改变执行它的数组。 mapping doesn't work because you have to copy the object and then extend the new properties映射不起作用,因为您必须复制 object 然后扩展新属性

If I understood your question correctly, please try the following example如果我正确理解了您的问题,请尝试以下示例

const object = {
  selectTag(selectedProduct, selectedTag) {
    const data = this.selectedProducts.filter((item) => {
      item.id === selectedProduct.id;
    });

    return data.map((entry) => ({
      ...entry,
      tagId: selectedTag.id,
      tagTitle: selectedTag.title,
    }));
  },
};

If this is in Vue.js, you cannot add properties to an object conventionally, as it will not be reactive.如果这是在 Vue.js 中,则不能按惯例将属性添加到 object,因为它不会反应。 You need to use Vue.set:你需要使用 Vue.set:

    selectTag: function(selectedProduct, selectedTag) {
      for (var i = 0; i < this.selectedProducts.length; i++) {
        if (this.selectedProducts[i].id === selectedProduct.id) {
          this.$set(this.selectedProducts[i], "tagId", selectedTag.id);
          this.$set(this.selectedProducts[i], "tagTitle", selectedTag.title);
        }
      }
    }

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

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