如何在对象中传递字符串时进行连词?

问题描述 投票:0回答:1

我遇到了一个问题,我需要把一个变量传入一个对象,所以它传入的是一个字符串。

this.context.updateCurrentValues({ "`${this.state.path}`": maskedvalue})

应该是这样传递的。

this.context.updateCurrentValues({ "prices.2.price": maskedvalue})

我认为在字符串连接方面出了点问题,记得我需要把一个对象传给函数。

谁能帮帮我?

javascript function object
1个回答
1
投票

EDIT.Methods.UpdateCurrentValues({ "${this.state.path}`": maskedvalue})应该像这样传递: ..:

为了得到你需要的字符串,你不想使用双引号。但如果你不使用引号,你会出现错误。所以你需要一步步的创建你的对象。

var myObject = {};
myObject[`${this.state.path}`] = maskedvalue;
this.context.updateCurrentValues(myObject);

=============================

老答案。

你不需要双引号。

this.context.updateCurrentValues({ `${this.state.path}`: maskedvalue})

0
投票

你忘了 []. 这应该可以。

this.context.updateCurrentValues({ ["prices.2.price"]: maskedvalue});
© www.soinside.com 2019 - 2024. All rights reserved.