0

I am going through an object and separating out the array in customer and customerportals but my code is not working

var j=0;
var k = 0;
var myVar[j][k] = Array();

$.each(Object.customer, function(index, value) { 

    $.each(value.portal.customerPortal, function(innerIndex, innerValue) { 

        myVar[j][k] = innerValue.name;
            k++;

        });
        j++;
    });

    alert(myVar[0][0]);

any help would be great

3
  • 1
    Did you really assign a customer property to Object? Also don't use Array(), use []. Commented Apr 20, 2011 at 23:50
  • 5
    "Is not working" is not enough. Commented Apr 20, 2011 at 23:50
  • 1
    Javascript is to Java as car is to carpet. Commented Apr 20, 2011 at 23:55

2 Answers 2

2

JavaScript is not Java. The syntax to declare an array is: someVariable = []

So:

var j=0;
var k = 0;
var myVar = [];
myVar[j] = [];

Which creates an array, containing one element at index 0 (which is another array)

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

4 Comments

how do i make it 2 dimenssional? I am confused
I guess I am trying to find how to get the values of myvar[j][k] out then and use them
@Autolycus: You don't. Javascript is not typed in that manner. You just add arrays as elements.
@Autolycus ([['foo'],['bar']])[1][0] -> "bar" Note that it is one operator [index], applied twice, to two different objects (the array containing the two arrays, and then the array containing 'bar'), just like in Java (neither Javascript nor Java have true n-dimensional arrays).
1

Your array declaration syntax is wrong (even, C-like!).

var myVar = [];

$.each(Object.customer, function(index, value) { 

    var newElm = [];
    $.each(value.portal.customerPortal, function(innerIndex, innerValue) { 
        newElm.push(innerValue.name);
    });

    myVar.push(newElm);
});

alert(myVar[0][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.