有没有一种干净的方法从对象中删除未定义的字段?
即
> var obj = { a: 1, b: undefined, c: 3 }
> removeUndefined(obj)
{ a: 1, c: 3 }
Run Code Online (Sandbox Code Playgroud)
我遇到了两个解决方案:
_.each(query, function removeUndefined(value, key) {
if (_.isUndefined(value)) {
delete query[key];
}
});
Run Code Online (Sandbox Code Playgroud)
要么:
_.omit(obj, _.filter(_.keys(obj), function(key) { return _.isUndefined(obj[key]) }))
Run Code Online (Sandbox Code Playgroud) 我有一节课:
public class A : INotifyPropertyChanged
{
public List<B> bList { get; set; }
public void AddB(B b)
{
bList.Add(b);
NotifyPropertyChanged("bList");
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Run Code Online (Sandbox Code Playgroud)
和绑定(UserControl的DataContext是A的实例):
<ListBox ItemsSource="{Binding Path=bList}" />
Run Code Online (Sandbox Code Playgroud)
显示元素,新对象添加到List后ListBox不会更新
将列表更改为ObservableCollection并删除NotifyPropertyChanged处理程序后,一切正常.
为什么列表不起作用?