0

Here's how I'm initializing and building an array:

var newCountyInfo = new Object();

newCountyInfo.name = newCountyName;
newCountyInfo.state = newCountyState;
newCountyInfo.zips = newCountyZips;
newCountyInfo.branchID = newCountyBranchID;

So I have my four elements in the array. I'm then passing newCountyInfo to another function to pull out the elements for display in some HTML elements.

The only way I know how to get to the individual elements in the function that uses them is this:

JSON.parse(JSON.stringify(newCountyValidation)).name
JSON.parse(JSON.stringify(newCountyValidation)).state
... etc...

There's got to be a better/shorter/more elegant way of doing this!

What is it?

2
  • 1
    That's not a "JSON array", that's a normal JavaScript object. You really should read about what JSON is (a data exchange format) and what JSON.parse and JSON.stringify are doing. Then you would notice that calling them like this is just unnecessary. Commented Feb 4, 2013 at 17:23
  • you would only need to use json if you were passing your object to say PHP or using it to create a cross domain api (jsonP). since you're not doing either of those, using a regular object literal {} would be the way to do it. Commented Feb 4, 2013 at 17:27

2 Answers 2

5

Why are you serializing at all? I don't understand what JSON has to do with this, unless you're using web workers, ajax, or something else which demands serialization. Start with object literal syntax:

var newCountyInfo = {
    name: newCountyName,
    state: newCountyState,
    zips: newCountyZips,
    branchID: newCountyBranchID
};

And just pass the whole object to the other function:

someOtherFunction(newCountyInfo);

Which can access the fields using plain old property accesses:

function someOtherFunction(foo) {
    console.log(foo.name); // whatever was in newCountyname
}

No JSON whatsoever.

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

4 Comments

I have to know, why were you shoveling the object through JSON like that? What made you think that was the right approach?
As it is apparently quite clear, I haven't messed with arrays too much, with the exception of getting an array of data from a PHP/MySQL return to an $.ajax call. In which case I use a JSON array. I assumed that this was just one way to handle arrays in Javascript in general. Now I know differently...
@Mathletics I am guessing the OP is (ab)using the term as PHP does, blurring the line between an object and an array.
@eventide unlike in PHP, a JS array is distinctly different from a JS object. I suggest you get a handle on that ASAP. developer.mozilla.org/en-US/docs/JavaScript/Guide/… and developer.mozilla.org/en-US/docs/JavaScript/Guide/…
1

Something like this should work just fine:

var newCountyInfo = {
    name: newCountyName,
    state: newCountyState,
    zips: newCountyZips,
    branchID: newCountyBranchID
}

function test(newCountyValidation)
{
    alert(newCountyValidation.name);
}

test(newCountyInfo);

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.