如何使用传播算子?

时间:2019-04-05 05:07:53

标签: reactjs

我正在研究React JS。我试图在这里使用传播算子。下面是我的代码。

const parametersQuickStoreSearch = (searchTerms, options, store) => {
}

在上面的代码中,searchTerms的值为simsKeycode:"35431081",存储的值为{value:all,label:all}。我想结合searchTerms的值并存储如下。

{ simsKeycode:"35431081", store: store.value }

我尝试如下

const storeParam = {
    'store': store.value,
}

此行引发我错误Unnecessarily quoted property 'store' found 有人可以帮我完成吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

下面是示例:

const person = { name: 'David Walsh', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };

const summary = {...person, ...tools};
/*
Object {
  "computer": "Mac",
  "editor": "Atom",
  "gender": "Male",
  "name": "David Walsh",
}
*/

答案 1 :(得分:0)

您可以尝试Object.assign

var searchTerms = {"simsKeycode":"35431081"}
var store = {"value":"all","label":"all"}
Object.assign({}, searchTerms, {store:store.value})

或传播算子

var searchTerms = {"simsKeycode":"35431081"}
var store = {"value":"all","label":"all"}
var storeParam = {store:store.value}
{...searchTerms, ...storeParam}

输出:

"{"simsKeycode":"35431081","store":"all"}"