如何使用lodash将字符串连接到现有数组元素

时间:2016-04-29 06:11:51

标签: json node.js lodash

我有一个json对象,其格式如下

"data": [{
     "firstName": "XYZ",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"Logo"
 }, {
     "firstName": "ABC",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"Logo1"
 }, {
     "firstName": "EFG",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"Logo2"
 }]

我希望以下输出使用nodejs

中的lodash将徽标名称与其路径连接起来
 "data": [{
     "firstName": "XYZ",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"E:/Logo"
 }, {
     "firstName": "ABC",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"E:/Logo1"
 }, {
     "firstName": "EFG",
     "lastName": "Admin",
     "userId": 1,
     "companyLogo":"E:/Logo2"
 }]

是否存在支持此类连接的任何函数?

1 个答案:

答案 0 :(得分:1)

不需要外部库:

var data = [
{
  "firstName": "XYZ",
  "lastName": "Admin",
  "userId": 1,
  "companyLogo":"Logo"
 },
 {
  "firstName": "ABC",
  "lastName": "Admin",
  "userId": 1,
  "companyLogo":"Logo1"
 },
  {
  "firstName": "EFG",
  "lastName": "Admin",
  "userId": 1,
  "companyLogo":"Logo2"
 }
]

var new_data = data.map(function(elem){ elem.companyLogo = "E: /" + elem.companyLogo; return elem });

console.log(new_data);