1

I have a json array with different key values and need to add a ServerUrl to the beginning of all node values using a loop without writing multiple statements to do that by using javascript:

"Urls": [
    { "getCar": "/getAllCars" },
    { "getPerson": "/getAllPersons" },
    { "getBook": "/getAllBooks" }
],
"ServerUrl": "http://192.168.1.1:3000"

The expected result must be:

"Urls": [
    { "getCar": "http://192.168.1.1:3000/getAllCars" },
    { "getPerson": "http://192.168.1.1:3000/getAllPersons" },
    { "getBook": "http://192.168.1.1:3000/getAllBooks" }
],

Any advice would be appreciated.

4
  • 1
    Can you add the expected output and what you tried? Commented Jul 25, 2019 at 20:58
  • 4
    There is no such thing as a "JSON Array" and arrays don't have key values (they have indexes - objects have keys). JSON is a string format for transporting data payloads over http/https. What you'll have is a JSON string, that will parse into a regular JavaScript array. And, in your case, that array will be populated with objects. Commented Jul 25, 2019 at 20:58
  • Also, it looks like you aren't really showing all the JSON. It looks like you are showing two keys of a higher level object. Commented Jul 25, 2019 at 21:00
  • 1
    Please read the usage description of the json tag. Commented Jul 25, 2019 at 21:02

4 Answers 4

4

You can use map to map your objects to new objects. Those objects have a single property, which you can get with Object.keys. The new object can get that same property name using the computed property name feature:

var obj = {
    "Urls": [
        { "getCar": "/getAllCars" },
        { "getPerson": "/getAllPersons" },
        { "getBook": "/getAllBooks" }
    ],
    "ServerUrl": "http://192.168.1.1:3000"
};

var urls = obj.Urls.map(o => Object.keys(o).map(k => ({ [k]: obj.ServerUrl + o[k] }))[0]);

console.log(urls);

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

Comments

1
const jsonVal = {
   "Urls": [
      { "getCar": "/getAllCars" },
      { "getPerson": "/getAllPersons" },
      { "getBook": "/getAllBooks" }
   ],
   "ServerUrl": "http://192.168.1.1:3000"
}

const result = jsonVal.Urls.map(val => 
   Object.keys(val).reduce((resultObj, endpointKey) => {
      resultObj[endpointKey] = `${jsonVal.ServerUrl}${val[endpointKey]}`;
      return resultObj;
   }, {})
);

Comments

1

Try (where your data are in d)

d.Urls.forEach( (x,i,a,k=Object.keys(x)[0]) => x[k] = d.ServerUrl + x[k]);

let d = {
  "Urls": [
    { "getCar": "/GetAllGroupCustomers" },
    { "getPerson": "/getAllItems" },
    { "getBook": "/GetAllCustomers" }
  ],
  "ServerUrl": "http://192.168.1.1:3000"
}

d.Urls.forEach( (x,i,a,k=Object.keys(x)[0]) => x[k] = d.ServerUrl + x[k]);

console.log(d);

Comments

1

A version that modifies your own object

var obj = {
    "Urls": [
        { "getCar": "/getAllCars" },
        { "getPerson": "/getAllPersons" },
        { "getBook": "/getAllBooks" }
    ],
    "ServerUrl": "http://192.168.1.1:3000"
};

obj.Urls.forEach(o => o[Object.keys(o)[0]] = `${obj.ServerUrl}${o[Object.keys(o)[0]]}`);

console.log(obj);

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.