1

I want to store details where the number of columns of array are fixed and number of rows are unknown. I want to assign each column a key by extracting text from the div of a specific page and then assign the value to it. This is my code

var data1=[[]];
var val=0;
$('.education_box.eb1 .inner_box').each(function(){
    $('.education_box.eb1 .inner_box ul li').each(function(){
        var key= $(this).children('strong').text();
        var value= $(this).children('em').text();
        data1[key][val]= value;
    });
    val++;
});
console.log(data1);

Please let me know how to declare the 2D array in line 1 as each time it gives the error: Cannot set property '0' of undefined

1
  • See the given answer Commented Dec 3, 2014 at 10:05

2 Answers 2

1

You can use:

var data1= [];
for (var i=0;i<$('.education_box.eb1 .inner_box ul li').length;i++) {
   data1[i] = [];
}

then use:

 data1[key][val]= value;
Sign up to request clarification or add additional context in comments.

1 Comment

where did you declare data1
0

Try this:

Declare array using Array class

var data=[];

data[0]=new Array(10,2);;
data[1]=new Array("val1","val2");
data[2]=new Array(true,false);

console.log(data);

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.