无法将JSON对象推入Array
Cannot push JSON object into Array
我知道它看起来像一个副本,但它不是。不管我怎么努力,都没用。
我有一个列表在我的angular模块:
this.checkedInterviews = []
,然后是一个函数:
var interviewModel = {
interviewId: self.pendingInterviews[i].id,
status: self.pendingInterviews[i].status,
location: self.pendingInterviews[i].location,
start: self.pendingInterviews[i].start,
hideCheck: null
};
this.checkedInterviews.push(JSON.stringify(interviewModel));
得到Cannot read property 'push' of undefined
。知道是什么问题吗?
var checkedIntervews = []
var interviewModel = {};
checkedIntervews.push(JSON.stringify(interviewModel));
console.log(checkedIntervews);
(function arrayPush() {
this.checkedIntervews = [];
var interviewModel = {};
this.checkedIntervews.push(JSON.stringify(interviewModel));
console.log(this.checkedIntervews);
})();
你想试试:
var checkedIntervews = []
var interviewModel = {};
checkedInterviews.push(JSON.stringify(interviewModel));
$scope.checkedInterviews = checkedInterviews; //If using AngularJS because Angular is tagged in the question
注意:如果所有这些都在同一个函数中,您应该能够使用this
。这也应该在全局范围内工作。我使用IIFE的唯一原因是将作用域
以上两种情况下添加的代码片段。
如果不清楚this
是什么在你的问题。
如果它们放入不同的函数,则第二个函数中的this
是不同的对象。
看来,第二个功能是使用不同的this
。
在Angular Module中,你可以将this
赋值给某个变量,然后尝试从第二个函数中访问它。
例如:
var vm = this;
vm.checkedInterviews = [];
现在在函数中,你应该使用:
vm.checkedInterviews.push();
试试这个这里是工作小提琴
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.checkedInterviews = [];
$scope.pendingInterviews = [{'id':1,'status':'pending','location':'abc','start':'start'}];
for(i= 0; i < $scope.pendingInterviews.length; i++){
var interviewModel = {
interviewId: $scope.pendingInterviews[i].id,
status: $scope.pendingInterviews[i].status,
location: $scope.pendingInterviews[i].location,
start: $scope.pendingInterviews[i].start,
hideCheck: null
};
console.log(interviewModel);
$scope.checkedInterviews.push(JSON.stringify(interviewModel));
}
console.log($scope.checkedInterviews);
}
相关文章:
- 如何为json对象中的段发送array[]
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- 为什么可以't我用Set对象调用Array.prototype.map
- 转换类似数组的对象Array.prototype.slice或Array.from
- 为什么 Array.prototype.reduce() 不接受 Map 对象作为初始值
- 如何在返回单个对象时递归使用 Array.prototype.find()
- ASP MVC 将 Array 对象发送到 JavaScript 函数
- 用Javascript将Array对象转换为Json
- 未能构造'Blob':提供的第一个参数为null,或者是无效的Array对象
- 在Javascript中向Array对象添加一个方法
- 在javascript中扩展Array对象的方法
- 若将Array对象添加到Array原型中,则不遍历该对象的函数
- 这是javascript中的错误吗?Array对象的方法推送
- 使用Array对象作为ES6 Map的键
- JSON计算特定Array对象的总数和结果分组
- 克隆“sub-array"对象数组(其中对象是深度嵌套的)的
- 从一个Array对象中提取关键值,并使用下划线javascript从其他对象中提取值进行筛选
- 在JavaScript中使用Array对象对数组内部的实例调用任意方法
- knockoutjs:绑定到一个索引号为特定Observable Array对象的模板
- 使用javascript或jquery将Array对象转换为JSON