简体   繁体   English

如何通过参数添加对象属性?

[英]How do I add an object property via an argument?

function addPropertyToProduct(product, property, value) {
  let tryThis = property;

  product.tryThis = value;

  return product;
}

The argument 'product' will be an object that looks like this:参数“product”将是一个如下所示的对象:

{ type: 'Terminator 2: Judgement Day', price: '£6.99', quantity: 1 }

Given a 'property' as an argument, as well as its corresponding value, update the 'product' to include this new information.给定一个“属性”作为参数,以及它的相应值,更新“产品”以包含这个新信息。 Then return the updated 'product'.然后返回更新的“产品”。

Eg if given the 'property' 'length' and the value '2h 36m', your function should return例如,如果给定 'property' 'length' 和值 '2h 36m',你的函数应该返回

{ type: 'Terminator 2: Judgement Day', price: '£6.99', quantity: 1, length: '2h 36m' }

This is the answer I'm receiving:这是我收到的答案:

 **+ expected** *- actual*

       {
      **+  "length": "2h 36m"**
         "price": "£6.99"
         "quantity": 1
      *-  "tryThis": "2h 36m"*
         "type": "Terminator 2: Judgement Day"
       }

This returns the value correctly but names the key as the variable name, not the argument itself?这会正确返回值,但将键命名为变量名,而不是参数本身?

Use square brackets [] :使用方括号[]

function addPropertyToProduct(product, property, value) {
  let tryThis = property;

  product[tryThis] = value;

  return product;
}

For more info, check: MDN Computed Property Names有关更多信息,请查看: MDN 计算属性名称

function addProperty(obj,propertyName,value){

      obj.propertyName = value;
      return obj;

}

This function will simply create a property dynamically and add value to it.此函数将简单地动态创建一个属性并为其添加值。 hope it helped.希望它有所帮助。

NB: you can also use the spread operator instead of assigning 'product' to a new variable inside your function.注意:您还可以使用扩展运算符,而不是将“产品”分配给函数内的新变量。 But, yes in general, "Computed Property Names" is the key as Alvaro is saying above:)但是,总的来说,“计算属性名称”是 Alvaro 上面所说的关键:)

function addPropertyToProduct(product, property, value) {
  return {
    ...product,
    [property]: value
  }
};

暂无
暂无

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

相关问题 如何在对象的属性上添加属性? - How do I add a property on a property of an object? 如何通过引用变量更改对象属性? - How do I change an object property via a reference variable? 当我将 object 分配给 object 的属性时,该属性会变成 object 吗? 如果是这样,我如何通过第二个访问第一个 object? - When I assign an object to a property of an object, does the property become an object? If so, how do I access the first object via the second one? 如何将名称属性添加到DOM输入复选框对象 - How do I add name property to a DOM input checkbox object 如何动态地将对象添加到对象的属性? - How do I dynamically add objects to an object's property? 如何通过另一个包含一个对象的对象访问一个对象,该对象是我要访问的对象的属性? - How do I access an object via another object that contains an object which is a property of the object I want to have access to? 如何有条件地向对象添加属性 - How can I add a property to an object conditionally 如何在Javascript中向“此”对象添加属性 - how can i add a property to 'this' object in Javascript 我可以向当前的“ this”对象添加属性吗? 怎么样? - Can I add a property to the current “this” object? How? 如何在不更改对象的情况下删除 1 个属性,以便向表格单元格添加内容? - How do I remove 1 property without changing object, so I can add content to tablecells?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM