1

I m new a web developer and i face up the following problem:

"Cannot read property 'length' of undefined"

my code:

var data=();

for(var i;i<parseInt(window.localStorage["numOfInserts"]);i++){
        data["category_name"]=localStorage.getItem(("category_name_"+i).toString());
        data["category_id"]=localStorage.getItem(("category_id_"+i).toString());
        data["provider_name"]=localStorage.getItem(("provider_name_"+i).toString());
        data["provider_id"]=localStorage.getItem(("provider_id_"+i).toString());
        data["appointment_date"]=localStorage.getItem(("appointment_date_"+i).toString());
        data["appointment_time"]=localStorage.getItem(("appointment_time_"+i).toString());
} 
$scope.allAppointments=dataArray;

for(var i=0;i<dataArray.length;i++){
       $scope.showme[i]=false;
}

After some research I understand that the problem caused to the fact that data is an array but I try to turn it to json, but

 var data ={};

gives me the same error as before.

Please Help me

3
  • 1
    what is dataArray? Commented Sep 28, 2015 at 8:28
  • var data=(); is a syntax error. "but I try to turn it to json" That's not JSON. Commented Sep 28, 2015 at 8:29
  • check dataArray object Commented Sep 28, 2015 at 8:31

3 Answers 3

2

I think this is what you're looking for, see code comments:

// Create an array using []
var data = [];

// Get the count once
var count = parseInt(window.localStorage["numOfInserts"]);

// Be sure to initialize `i` to 0
for (var i = 0; i < count; i++) {
    // Create an object to push onto the array, using the information
    // from local storage. Note that you don't need toString() here.
    // Once we've created the object (the {...} bit), we push it onto
    // the array
    data.push({
        category_name: localStorage.getItem("category_name_"+i),
        category_id: localStorage.getItem("category_id_"+i),
        provider_name: localStorage.getItem("provider_name_"+i),
        provider_id: localStorage.getItem("provider_id_"+i),
        appointment_date: localStorage.getItem("appointment_date_"+i),
        appointment_time: localStorage.getItem("appointment_time_"+i)
    });
} 

This does the same thing, it's just more verbose and so could help you understand more clearly what's going on:

// Create an array using []
var data = [];

// Get the count once
var count = parseInt(window.localStorage["numOfInserts"]);

// Be sure to initialize `i` to 0
for (var i = 0; i < count; i++) {
    // Create an object to push onto the array
    var obj = {};

    // Fill it in from local storage. Note that you don't need toString() here.
    obj.category_name = localStorage.getItem("category_name_"+i);
    obj.category_id = localStorage.getItem("category_id_"+i);
    obj.provider_name = localStorage.getItem("provider_name_"+i);
    obj.provider_id = localStorage.getItem("provider_id_"+i);
    obj.appointment_date = localStorage.getItem("appointment_date_"+i);
    obj.appointment_time = localStorage.getItem("appointment_time_"+i);

    // Push the object onto the array
    data.push(obj);
} 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to create an array(dataArray before the loop), and create a new object in each iteration and set the property values for that object then add the object to the array like below

var dataArray = [],
    data, numOfInserts = parseInt(window.localStorage["numOfInserts"]);

for (var i = 0; i < numOfInserts; i++) {
    data = {};
    data["category_name"] = localStorage.getItem(("category_name_" + i).toString());
    data["category_id"] = localStorage.getItem(("category_id_" + i).toString());
    data["provider_name"] = localStorage.getItem(("provider_name_" + i).toString());
    data["provider_id"] = localStorage.getItem(("provider_id_" + i).toString());
    data["appointment_date"] = localStorage.getItem(("appointment_date_" + i).toString());
    data["appointment_time"] = localStorage.getItem(("appointment_time_" + i).toString());
    dataArray.push(data)
}
$scope.allAppointments = dataArray;

for (var i = 0; i < dataArray.length; i++) {
    $scope.showme[i] = false;
}

Comments

0

It looks like you're trying to create an associative array, so the first line should indeed be

var data = {};

The next part is fine, but then it looks like you want to enumerate the keys

for(var i=0;i<Object.keys(data).length;i++){
       $scope.showme[i]=false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.