0

In my previous question I asked once, but I still didn't get my problem solved. I want to change key and append value of Object in javascript as below :

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

I want to change like this:

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

Thank for help

1
  • use map() method Commented Mar 23, 2018 at 4:38

2 Answers 2

5

Use .map:

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

console.log(resp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.