js数组在json声明后未定义

时间:2015-09-28 08:26:41

标签: javascript json angularjs

我是一名新的网络开发人员,我遇到了以下问题:

“无法读取未定义的属性'长度'

我的代码:

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;
}

经过一些研究后我明白问题是因为数据是一个数组,但我试图把它变成json,但是

 var data ={};

给了我与以前相同的错误。

请帮帮我

3 个答案:

答案 0 :(得分:2)

我认为这正是您所需要的,请参阅代码注释:

// 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)
    });
} 

这也是同样的事情,它更加冗长,因此可以帮助您更清楚地了解正在发生的事情:

// 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);
} 

答案 1 :(得分:1)

您需要在循环之前创建一个数组(dataArray),并在每次迭代中创建一个新对象并设置该对象的属性值,然后将该对象添加到数组中,如下所示

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;
}

答案 2 :(得分:0)

看起来你正在尝试创建一个关联数组,所以第一行应该是

var data = {};

下一部分很好,但是看起来你想要枚举

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