在javascript中更改对象的键和附加值的最佳方法是什么?

时间:2018-03-23 04:37:13

标签: javascript object

previous question我曾问过一次,但我仍然没有解决问题。我想在javascript中更改关键字和Object的值,如下所示:

var dataObj = 
 [
  {
    "image": "a.jpg"
  },
  {
    "image": "b.png"
  },
  ..................
  ..................
];

我想改变这样:

dataObj = 
     [
      {
        "src": "stackoverfloow.com/uploads/a.jpg"
      },
      {
        "src": "stackoverfloow.com/uploads/b.png"
      },
      ..........................................
      ..........................................
    ];

感谢您的帮助

2 个答案:

答案 0 :(得分:4)

使用.map



const data1 = [ 
  { "image"  : "a.jpg" },
  { "image"  : "b.png"},
];
const data2 = data1.map(({ image }) => ({ src: 'stackoverfloow.com/uploads/' + image }));
console.log(data2);




答案 1 :(得分:0)



var dataObj = [{ "image": "a.jpg" }, { "image": "b.png" }];

let resp = dataObj.map(x => ({"src": "stackoverfloow.com/uploads/" + x.image}));

console.log(resp);