根据文档,最新节点(节点5+)默认应支持扩展运算符,如下所示:
const newObj = {
...oldObj,
newProperty: 1
}
Run Code Online (Sandbox Code Playgroud)
我安装了节点5.10.1(例如'node -v'告诉我的那个).但我仍然收到此错误:
c:\[myprojectfolder]>node index.js
c:\[myprojectfolder]\index.js:21
...oldObj,
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
这是我的json:
[ { gsm: 'gsm',
firstName: 'firstname',
lastName: 'lastname' },
{ gsm: '123456789',
firstName: 'Mohamed',
lastName: 'Sameer'},
{ gsm: '987654321',
firstName: 'Hameed',
lastName: 'Basha'}]
Run Code Online (Sandbox Code Playgroud)
我希望我的最终输出看起来像:
[ { gsm: 'gsm',
firstName: 'firstname',
lastName: 'lastname',
userKey: 'Key1'},
{ gsm: '123456789',
firstName: 'Mohamed',
lastName: 'Sameer',
userKey: 'Key1'},
{ gsm: '987654321',
firstName: 'Hameed',
lastName: 'Basha',
userKey: 'Key1'}]
Run Code Online (Sandbox Code Playgroud)
我需要使用任何javascript方法,如map等
我正在尝试向数组中的每个对象添加一个属性:
var capitals = [{
country: "What's the capital of Ecuador?",
choices: ["Quito", "Caracas", "Panama", "Loja"],
corAnswer: 0
}, {
country: "What is the capital of China?",
choices: ["Shanghai", "Beijing", "Ghangzou", "Hong Kong"],
corAnswer: 0
}];
Run Code Online (Sandbox Code Playgroud)
应该成为:
var capitals = [{
country: "What's the capital of Ecuador?",
choices: ["Quito", "Caracas", "Panama", "Loja"],
corAnswer: 0,
global: true
}, {
country: "What is the capital of China?",
choices: ["Shanghai", "Beijing", "Ghangzou", "Hong Kong"],
corAnswer: 0,
global: true
}];
Run Code Online (Sandbox Code Playgroud)
该数组可以包含多个对象.我需要什么样的功能?
我在没有角度或下划线的节点工作,我正在寻找一个有效的普通javascript答案(也许避免循环).我有以下JSON对象数组:
object = [
{account:2423, user:1564},
{account:1235, user:1564}
]
Run Code Online (Sandbox Code Playgroud)
我想将以下内容添加到对象中的每个数组:
username:"clientname"
Run Code Online (Sandbox Code Playgroud)
所以它最终看起来像这样:
object = [
{account:2423, user:1564, username:"clientname"},
{account:1235, user:1564, username:"clientname"}
]
Run Code Online (Sandbox Code Playgroud)
这个问题要求避免循环.