【发布时间】:2015-05-13 14:22:21
【问题描述】:
我有以下对象数组:
$scope.users = [
{
ID: "1",
Name: "Hege",
Username: "Pege",
Password: "hp",
},
{
ID: "2",
Name: "Peter",
Username: "Pan",
Password: "pp"
}
];
我需要用这样的空值创建一个类似的对象,
$scope.newUser = {
ID: "",
Name: "",
Username: "",
Password: ""
}
这样我就可以将它推送到同一个数组 ($scope.users.push($scope.newUser);),让它看起来像这样:
$scope.users = [
{
ID: "1",
Name: "Hege",
Username: "Pege",
Password: "hp"
},
{
ID: "2",
Name: "Peter",
Username: "Pan",
Password: "pp"
},
{
ID: "",
Name: "",
Username: "",
Password: ""
}
];
但是,数组$scope.users 并不总是包含相同对象的数组。即使我将数组更改为不同的东西,我也需要它工作,例如,像这样:
$scope.users = [
{
SID: "pepe",
Name: "Peter",
School: "Primary School"
},
{
SID: "hepe",
Name: "Hege",
School: "Junior School"
}
];
我该怎么做?
【问题讨论】:
-
你确定数组中只有具有相同结构的对象吗?无论如何,角度不提供清理对象字段的方法,但您可以简单地编写自己的函数
-
你为什么要这样做?我知道这不是一个答案,但它似乎违反了我所说的良好编程。让一组事物包含未定义的事物气味。
标签: javascript arrays json angularjs