0

I'm trying to separate some values that come from an AJAX request.
They look like this:

Object { name: "fsdfsd", details: "sdfsdf", password: "sdfsdgds" }

And I'd basically need them separated each value in its variable. How can I do that?
I've searched around but can't find a good answer. I'm used to PHP syntax like $this->output and I just moved to using JS more than once in a month so I'm still trying to accomodate :)
Thank you!

5
  • Do you get that string on the server side or on the client side? So where do you want to "extract" those values? Commented Jun 28, 2016 at 6:50
  • @arkascha I get them in the view through an AJAX request that calls a controller (working in CodeIgniter). This is the content I need but now I have to populate a few inputs, each with its value Commented Jun 28, 2016 at 6:51
  • That did not answer my question, sorry. Commented Jun 28, 2016 at 6:53
  • I'm getting that from server side and I need to "print" it on client side. I get it as JSON Object and now I have to separate the values. Commented Jun 28, 2016 at 6:55
  • So the answer is: on the client side. OK, then the answers below should be what you are looking for. Commented Jun 28, 2016 at 6:56

6 Answers 6

3

Try this:

var res = { name: "fsdfsd", details: "sdfsdf", password: "sdfsdgds" };
var name = res.name;
var details = res.details;
var password = res.password;

But I strongly recommend you to use res as object. You could use less number of variables and have less potential problems. AND DON'T SEND PASSWORD AS A PLAIN TEXT! Someone can listen your traffic and dump all your users passwords.

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

1 Comment

I know about security problems with sending passwords, still, I'm working on a small internal platform where employees store simple passwords like for Windows, for Skype etc. so nothing to worry about. Thanks for the heads up though!
1

Here is a ES6 version if you want

It uses Destructuring assignment. It will only work in the latest browsers

var res = { name: "fsdfsd", details: "sdfsdf", password: "sdfsdgds" }

var {name, details, password} = res

console.log(name)

Comments

0
var objectName = { name: "fsdfsd", details: "sdfsdf", password: "sdfsdgds" }
var name = objectName.name;
var details = objectName.details;
var password = objectName.password;

Comments

0

try this:

var res = {
  name: "fsdfsd",
  details: "sdfsdf",
  password: "sdfsdgds"
}
var Obj = res.name + " " + res.details + " " + res.password
console.log(Obj)

1 Comment

OP wants to destruct the object to separate variables. So this is not the answer he is looking for
0

You could also use with statement. It extends the scope chain for a statement.

var res = { name: "fsdfsd", details: "sdfsdf", password: "sdfsdgds" }

with(res){
  console.log(name)
  console.log(details)
  console.log(password)
}

Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable. Ref

Comments

0

you don't need to separate them. by the look of it the ajax request is returning a json object. store it a variable. ex:

var returnObj = getServicerequest(input); // ajax request that you call is handled in this function.

now you can access the value pairs like:

alert(returnObj.name);
alert(returnObj.datails);

however returning username and passwords via ajax calls are not a good security practise.

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.