繁体   English   中英

将名称从属性分配给 object

[英]Assign name from property to object

我正在尝试创建一个 object,它的名称来自另一个属性。

const slide = 0;
const result = 'Hello';
const object = { slide: result };

How I want the object to look like in the end:
{ 0: result }

我怎样才能做到这一点? 因为我使用它的方式,名字总是“slide”,但我希望它以0作为名字。

您可以执行以下操作:

const object = { [slide]: result };

或另一种方法是:

const object = {};
object[slide] = result;

类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object>

[英]Type Error: Cannot assign to read only property 'backgroundColor' of object '#<Object>'

暂无
暂无

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

相关问题 动态分配属性名称给对象 通过AJAX分配和使用javascript对象属性 如何从 object 获取名称并将其分配给新创建的元素? 类型错误:无法分配给 object 的只读属性 'backgroundColor' '#<object> '<div id="text_translate"><p> 我正在使用 React 创建一个组件,该组件将根据其属性呈现不同的背景。 但是,我不知道如何在不折叠 React 框架的情况下更改其属性。 这是我的代码:</p><pre> const inactiveStyle = { backgroundColor: "", color: "white", }; const activeStyle = { color: "black" }; export default class DrumPad extends Component { constructor(props) { super(props); this.state = { style: inactiveStyle, }; render(){ inactiveStyle.backgroundColor = this.props.backgroundColor return (&lt;div style={this.state.style}&gt;&lt;/div&gt;)</pre><p> 当我尝试它时,这是我得到的错误:</p><p> TypeError:无法分配给 object '#' 的只读属性 'backgroundColor'</p><p> 我已尝试按照<a href="https://stackoverflow.com/questions/52143027/reactjs-typeerror-cannot-assign-to-read-only-property-transform-of-object" rel="nofollow noreferrer">此处</a>的建议进行操作,但它不起作用,因为我的原始代码已经是一个常量,而不是 state 本身。 请帮忙。</p></div></object> 错误类型错误:无法分配给“12345”上的属性“验证器”:不是对象 未捕获的类型错误:无法分配给 object 的只读属性 'tagName' '#<htmlimageelement> '</htmlimageelement> 如何动态设置JS对象属性名称 将 JSON 中的数据直接分配给 Angular 中的 object 如何使用属性名称在运行时动态获取对象属性 尝试将查询字符串中的值分配给Shopify订单项属性
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM