我想知道在lodash中是否有一个更简单的方法来替换JavaScript集合中的项目?(可能重复,但我不明白那里的答案:)
我看了他们的文档但找不到任何东西
我的代码是:
var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];
// Can following code be reduced to something like _.XX(arr, {id:1}, {id:1, name: "New Name"});
_.each(arr, function(a, idx){
if(a.id === 1){
arr[idx] = {id:1, name: "Person New Name"};
return false;
}
});
_.each(arr, function(a){
document.write(a.name);
});
Run Code Online (Sandbox Code Playgroud)
更新: 我正在尝试替换的对象有许多属性,如
{id:1,Prop1:...,Prop2:...,依此类推}
解:
感谢dfsq,但我在lodash中找到了一个合适的解决方案,似乎工作正常并且非常整洁,我把它放在mixin中,因为我在很多地方都有这个要求.JSBin
var update = function(arr, key, newval) {
var match = _.find(arr, key);
if(match)
_.merge(match, newval);
else
arr.push(newval);
};
_.mixin({ '$update': …
Run Code Online (Sandbox Code Playgroud) 我正在运行IIS 7集成模式,我正在使用
在此上下文中不提供请求
当我尝试在调用的Log4Net相关函数中访问它时Application_Start
.这是我的代码行
if (HttpContext.Current != null && HttpContext.Current.Request != null)
Run Code Online (Sandbox Code Playgroud)
并且正在抛出异常以进行第二次比较.
除了检查HttpContext.Current.Request for null之外,我还能检查什么?
在iis7.5上运行mnc时,在此上下文异常中发布了类似的问题 @Request不可用
但也没有相关的答案.
我有这个包含数百列的巨大视图,我需要选择第 114 列,例如:
SELECT "144" FROM MyView;
Run Code Online (Sandbox Code Playgroud)
PS:显然,我不知道该列的名称。我只是将结果行复制到一个 Excel 文件中,搜索了 EJ 列中的特定值,因此我想选择我视图中仅显示第 n 列的所有行以进行进一步调试。
干杯!
我试过这里提到的答案,但没有运气(我正在使用angularjs 1.3).我的问题是两部分
1)尽管在范围内使用了'='(参见下面的代码),但复杂属性不会作为对象传递
2)应该评估以提供单向绑定的函数也作为字符串传递
样品使用,
<button type="button" class="btn btn-success" ng-click="RestEditCtrl.saveRestaurantDetails();">
<smart-btn-label btn-state="RestEditCtrl.ajaxState(RestEditCtrl.restaurant.id)"
when-normal="{label: 'Save', icon: 'glyphicon-floppy-disk' }"
when-active="{label: 'Saving...', icon: 'glyphicon-refresh glyphicon-spin-animate'}"
when-success="{label: 'Saved!', icon: 'glyphicon glyphicon-floppy-saved'}"
when-error="{label: 'Try saving again', icon: 'glyphicon glyphicon-exclamation-sign'}"></smart-btn-label>
</button>
Run Code Online (Sandbox Code Playgroud)
指令代码,
angular.module("app")
.directive('smartBtnLabel', function () {
return {
restrict: 'E',
scope: {
btnState: '&', // not working, @ evaluates but that defeats my purpose
whenActive: '=', // not evaluating any which way, it always comes as string
whenError: '=',
whenSuccess: '=', …
Run Code Online (Sandbox Code Playgroud) html javascript angularjs angularjs-directive angularjs-scope