0

Here method is dynamic based on user input method value will be change

var method = 'GetActiveUserData';

How can i concatenate ? please help below expected output added

var result =  data["s:En"]["s:Body"][0].`${method}`Response[0].`${method}`Result[0]["a:datas"][0]["b:data"];

Expected output:

data["s:En"]["s:Body"][0].GetActiveUserDataResponse[0].GetActiveUserDataResult[0]["a:datas"][0]["b:data"];
2
  • + is string concatenation in javascript Commented May 6, 2020 at 14:33
  • Look into this question it may have your answer : Javascript dot operator inside strings Commented May 6, 2020 at 14:35

2 Answers 2

3

Use [] on objects to access properties based on an expression:

var result = data
  ["s:En"]
  ["s:Body"][0]
  [`${method}Response`][0]
  [`${method}Result`][0]
  ["a:datas"][0]
  ["b:data"];

obj['blah'] is logically equivalent to obj.blah, so if method is GetActiveUserData, obj[`${method}Response`] is equivalent to obj.GetActiveUserDataResponse.

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

Comments

0

Is this what you want?

Template string is available in most browser

var result =  `data["s:En"]["s:Body"][0].${method}Response[0].${method}Result[0]["a:datas"][0]["b:data"]`;

But if it doesn't work, you may try a simple string concatenate:

var result =  'data["s:En"]["s:Body"][0].' + method + 'Response[0].' + method + 'Result[0]["a:datas"][0]["b:data"]';

2 Comments

var rawAllocations = ata["s:En"]["s:Body"][0].${method}Response[0].${method}Result[0]["a:datas"][0]["b:data"]; ^ SyntaxError: Unexpected token {
Template string should be available in most browser but if it doesn't work, you may use a simple javascript string concatenate. I'll update my answer

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.