Jump to content
iwato

Javascript Object to Array Conversion

Recommended Posts

I recently used PHP's json_encode( ) function to covert an array created with PHP into a JSON encoded object, as the conversion appeared normal,  I then assigned the value to a Javascript variable using PHP's echo construct and reproduced it with a JS alert.  This too, appeared normal. My difficulty arose when I tried using the jQuery .map( ) method to convert my nested Javascript object into a nested Javascript array.  I received the following error message in my Console:

TypeError: e is not an Object. (evaluating 't-1 in e')

Am I being foolish to believe that I need a nested array when I already have a nested object?  If not, is there not a routine to perform this very conversion?

Edited by iwato

Share this post


Link to post
Share on other sites

What does your code look like?

The map() function runs a callback on each of the elements in an array or object. I don't think it works recursively.

Share this post


Link to post
Share on other sites

Yes, I am of the same mind.  My problem is that I have no experience working with objects in Javascript -- even though everything in the end is an object anyway.  I recently found the following routine on the net and am going to try it.  It appears to be written for two layers and thus suits my purpose well.  The fact there is no easily found routine for recursive cloning of nested objects to nested arrays, also suggests that there is little need.  And, it makes me feel a little stupid about my knowledge of nested objects and how to work with them in Javascript.

for (var i in data) {
    var item = data[i];
    var outer = [];
    if (typeof item === "object") {
        for (var j in item) {
            var temp = [];
            temp.push(j);
            temp.push(item[j]);
            outer.push(temp);
        }
    }
    if (outer.length) {
        chartData.push(outer);
	}
}

Roddy

Share this post


Link to post
Share on other sites

Here's a question from StackOverflow addressing copying an object: http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript

The best solution seems to be to convert to a JSON string and then converting back to an object.

var x = { a : 1, b : 2 };
var y = JSON.parse(JSON.stringify(x));

I've hardly ever come across a situation in Javascript where I needed to clone an object.

Share this post


Link to post
Share on other sites

QUESTION:  How did I finally resolve the problem?

ANSWER:  I abandoned the idea of creating a Javascript array, learned a little more jQuery, and opted for the jQuery $.each( ) method that iterates over array-objects and objects with seeming indifference.

Note:  $wordmax is a nested, associative PHP array

	var wordmax = '<?php echo json_encode($wordmax)?>;';
	var wordmax_json = JSON.parse(wordmax);
	$.each(wordmax_json, function(property, textobj) {
		$.each(textobj, function(name, val) {
			alert(name + ': ' + val);
		});
	});

As always, thank you for your many suggestions and leads, Ingolme.

Share this post


Link to post
Share on other sites

Considering JSON is literally Javascript, you don't need to put it into a string to use it. You can directly do this and skip the parsing:

var wordmax_json = <?php echo json_encode($wordmax)?>;

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×