0

There are many sms sending service providers and all of these services have different parameter names. I do not know which company is using which web service provider. If I request the parameters to be requested from the user during the soap request, is it possible to define these parameters as object keys?

For example:

First user request parameters : [companyName ,msgBody,recipients],

Another user request parameters: [id,msgText,numbers,channelCode],

If we have defined this array to the function parameter that will send the sms request.

And I want to create Object for Soap Body.

Result for First user : obj={companyName:'ASDDASD',msgBody:'Hello',recipients:'0264344534'}

Result for Another user : obj ={id:21,msgText:'hi,numbers:54564234,channelCode:1}

Note: obj values ​​are written randomly

2
  • Is the order of values in the array always the same? Commented Jun 10, 2020 at 11:36
  • Yes, that wil be the same Commented Jun 10, 2020 at 11:37

2 Answers 2

2

Use brackets to reference property names: obj['propname']
If you want to use a variable for the key name in an object literal, you can use ES2015 computed property names: { [keyname]: 'value' }

keys1 = ['companyName', 'msgBody','recipients']
vals1 = [1, 2, 3]

keys2 = ['id','msgText','numbers','channelCode']

const obj1 = {}, obj2 = {}
keys1.forEach((key,i) => obj1[key]=vals1[i])
keys2.forEach(key => obj2[key]=key+'value')

console.log(JSON.stringify(obj1), JSON.stringify(obj2))

console.log(JSON.stringify(
{
 [keys1[0]]: 'value'
}
))

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

1 Comment

keys2.forEach(key => obj2[key] = `${key}value`)
0

You would need a function like this? Assuming the parameters are strings (['a','b'], not [a,b]) and the objects are an array of javascript objects.

/* function projectObjects
@params: an array of string parameters
@objects: an array of objects with these parameters as properties
Projects the objects on the parameter list and returns an array of objects with all and only params as properties */
function projectObjects(params,objects) {

   let projectOne = function(o) {
       let result = {};
       params.forEach(
          (p) => {
             // check the behaviour when o[p] is undefined
             result[p] = o[p];
          }
       )
       return result;
   }
   
   results = objects.map(projectOne);
   return results;

}

console.log(projectObjects(['roses'],[{'roses':1,'violets':2},{'roses':3,'begonias':22},{'daisies':0}]));

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.