1 javascript arrays json object
这是我的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等
使用 map
var output = arr.map( s => ( s.userKey = "Key1", s ) );
Run Code Online (Sandbox Code Playgroud)
演示
var arr = [{
gsm: 'gsm',
firstName: 'firstname',
lastName: 'lastname'
},
{
gsm: '123456789',
firstName: 'Mohamed',
lastName: 'Sameer'
},
{
gsm: '987654321',
firstName: 'Hameed',
lastName: 'Basha'
}
];
var output = arr.map( s => ( s.userKey = "Key1", s ) );
console.log( output );Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |