AngularJS检查下载的json是否在工作副本之间有差异
AngularJS check if downloaded json has difference between working copy
我下载json,所以:
$scope.getarticles = function(company) {
$http.get("url", {
headers: {
'Content-Type': 'application/json',
'If-Modified-Since': ''
}
})
.success(function(response) {
$.each(response, function(ind, el) {
$scope.articles.push(el);
});
})
.error(function(err, status) {
});
};
我使用我的 $scope.articles 工作,对于某些对象,我可以更改数据(注意,它也可以有排序 uid 等,不要比较它们) - 如何将我的数据与我从 JSON 获得的数据进行比较?
$.each($scope.articles, function(ind, el) {
if (el == $scope.copyArticles)
console.log("yes");
});
真的以某种方式做吗?如果是,那怎么做?
没有简单的内置方法,特别是如果您必须忽略某些字段,例如排序 UID。
它只是回到基本的Javascript。
例如,定义一个函数来比较两篇文章是否相等,忽略任何您不关心的字段。为了方便起见,我使用 Lodash 或 UnderscoreJS 的 _.omit()
函数来删除我不关心的键,并使用 angular.equals()
对结果对象进行深入比较。
function articlesAreEqual(article1, article2) {
return angular.equals(strippedArticle(article1), strippedArticle(article2));
}
function strippedArticle(a) {
return _.omit(a, 'sortUID' /* add other keys here */);
}
然后,您可以查看是否有任何下载的与现有版本匹配:
$scope.getarticles = function(company) {
$http.get("url", {
headers: {
'Content-Type': 'application/json',
'If-Modified-Since': ''
}
})
.success(function(response) {
$.each(response, function(ind, el) {
if (articlesAreEqual($scope.articles[ind], el)) {
console.log('Article #' + (ind + 1) + ' is the same.');
} else {
console.log('Article #' + (ind + 1) + ' differs.');
$scope.articles.splice(ind, 1, el); // replace the existing one
}
});
})
.error(function(err, status) {
});
};
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 全局变量和全局对象的属性之间有什么区别吗
- java.net和javascript之间正则表达式的差异
- JavaScript中的函数和对象之间没有区别吗?
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- Jquery在函数之间传递表行
- 根据某些条件在视图之间切换
- 在控制器和数据对象之间同步数据
- d3中堆栈函数和嵌套函数之间的差异
- JQuery:在页面之间滑动
- 如何使用JavaScript查找1和N之间的所有数字的总和
- 操作放置在画布上的元素之间的连接
- 在下划线中使用_(obj).map(callback)和_.map(obj,callback)之间的区别
- jquery在表单之间切换
- Nodejs API控制器,用于在API之间切换
- 如何在aspx页面之间传递参数
- 在索引.html和应用.js [node.js] 之间共享变量
- 什么是&&在没有if的行中的变量之间
- DOM元素和angular元素之间的主要区别是什么
- AngularJS检查下载的json是否在工作副本之间有差异