在敲除可观察数组上使用 underscore.js "pluck"
Use underscore.js "pluck" on a knockout observable array
我有一个可观察的对象数组,我想使用 underscore.js
提取值
例如:
ko.observableArray([{
id: ko.observable(1),
name: ko.observable("name1")
},
{
id: ko.observable(2),
name: ko.observable("name2")
},
...])
我只想提取对象内部的值而不是整个可观察值。
我可以只用一个命令来完成吗?
我试过了:
_.pluck(myArray(), "id()")
和 _.pluck(myArray(), "id"())
但是这些 return 一个未定义数组和 "id is not a function" 分别。
谢谢!
我使用 "map" 函数解决了这个问题:
_.map(myArray(), function(item) {return item.id()});
但我希望使用 pluck,因为它正是此类场景的用例。
因为name
是一个函数,如何pluck
把你原来的数组变成一个函数数组,再用ko.toJS
转成字符串数组呢?
var myArray = ko.observableArray([{
id: ko.observable(1),
name: ko.observable("name1")
},
{
id: ko.observable(2),
name: ko.observable("name2")
}]);
var names = _.pluck(myArray(), 'name');
console.log(ko.toJS(names)); // Output: ["name1", "name2"]
简答
使用_.invoke
代替_.pluck
长答案
_.pluck(list, propertyName)
按照记录工作:
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
或者,正如在 lodash 文档中更好地解释的那样:_.pluck(collection, path)
Gets the property value of path from all elements in collection.
所以,如果你这样做:
_.pluck(myArray(), "id")
您得到的是一个包含所有 id
的数组。所有这些 id
都是可观察的,就像在原始数组
的对象中一样
但您可以使用 _.invoke(list, methodName, *arguments)
,如文档所述:
Calls the method named by methodName on each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.
或者,在 lodash 版本上 _.invoke(collection, path, [args])
Invokes the method at path on each element in collection, returning an array of the results of each invoked method. Any additional arguments are provided to each invoked method. If methodName is a function it is invoked for, and this bound to, each element in collection.
通过这种方式,您执行每个可观察对象,并按预期获得其值:
_.invoke(myArray(), "id")
注意充满可观察对象的视图模型!
这个问题的第一条评论让我加入了这个通知:
最佳解决方案是使用 ko.toJS
将视图模型中的所有可观察对象转换为具有常规属性的常规 JavaScript 对象。执行此操作后,underscore 或任何其他库将按预期工作。
_.invoke
解决方案仅适用于单一级别的可观察对象,如本例所示。如果有多层嵌套的可观察对象,它将完全失败,因为它在路径的末尾调用一个函数,而不是在路径的每一步,例如,_.invoke
不适用于这种情况:
var advices = [{
person: ko.observable({
name = ko.observable('John')
}),
advice: ko.observable('Beware of the observables!')
}];
在这种情况下,您只能在第一层使用 _.invoke
,例如:
var sentences = _.invoke(advices,'advice');
但这行不通:
var names = _.invoke(advices,'person.name');
在这个调用中,只有 name
会被调用,但 person
不会,所以这会失败,因为 person
是一个可观察的,因此它没有 name
属性.
注意:lodash 是另一个类似的库,主要与下划线兼容,但在某些方面更好
先打开它
_.pluck(ko.toJS(myArray), 'id')
_(ko.toJS(myArray)).pluck('id)
我有一个可观察的对象数组,我想使用 underscore.js
提取值例如:
ko.observableArray([{
id: ko.observable(1),
name: ko.observable("name1")
},
{
id: ko.observable(2),
name: ko.observable("name2")
},
...])
我只想提取对象内部的值而不是整个可观察值。
我可以只用一个命令来完成吗?
我试过了:
_.pluck(myArray(), "id()")
和 _.pluck(myArray(), "id"())
但是这些 return 一个未定义数组和 "id is not a function" 分别。
谢谢!
我使用 "map" 函数解决了这个问题:
_.map(myArray(), function(item) {return item.id()});
但我希望使用 pluck,因为它正是此类场景的用例。
因为name
是一个函数,如何pluck
把你原来的数组变成一个函数数组,再用ko.toJS
转成字符串数组呢?
var myArray = ko.observableArray([{
id: ko.observable(1),
name: ko.observable("name1")
},
{
id: ko.observable(2),
name: ko.observable("name2")
}]);
var names = _.pluck(myArray(), 'name');
console.log(ko.toJS(names)); // Output: ["name1", "name2"]
简答
使用_.invoke
代替_.pluck
长答案
_.pluck(list, propertyName)
按照记录工作:
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
或者,正如在 lodash 文档中更好地解释的那样:_.pluck(collection, path)
Gets the property value of path from all elements in collection.
所以,如果你这样做:
_.pluck(myArray(), "id")
您得到的是一个包含所有 id
的数组。所有这些 id
都是可观察的,就像在原始数组
但您可以使用 _.invoke(list, methodName, *arguments)
,如文档所述:
Calls the method named by methodName on each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.
或者,在 lodash 版本上 _.invoke(collection, path, [args])
Invokes the method at path on each element in collection, returning an array of the results of each invoked method. Any additional arguments are provided to each invoked method. If methodName is a function it is invoked for, and this bound to, each element in collection.
通过这种方式,您执行每个可观察对象,并按预期获得其值:
_.invoke(myArray(), "id")
注意充满可观察对象的视图模型!
这个问题的第一条评论让我加入了这个通知:
最佳解决方案是使用 ko.toJS
将视图模型中的所有可观察对象转换为具有常规属性的常规 JavaScript 对象。执行此操作后,underscore 或任何其他库将按预期工作。
_.invoke
解决方案仅适用于单一级别的可观察对象,如本例所示。如果有多层嵌套的可观察对象,它将完全失败,因为它在路径的末尾调用一个函数,而不是在路径的每一步,例如,_.invoke
不适用于这种情况:
var advices = [{
person: ko.observable({
name = ko.observable('John')
}),
advice: ko.observable('Beware of the observables!')
}];
在这种情况下,您只能在第一层使用 _.invoke
,例如:
var sentences = _.invoke(advices,'advice');
但这行不通:
var names = _.invoke(advices,'person.name');
在这个调用中,只有 name
会被调用,但 person
不会,所以这会失败,因为 person
是一个可观察的,因此它没有 name
属性.
注意:lodash 是另一个类似的库,主要与下划线兼容,但在某些方面更好
先打开它
_.pluck(ko.toJS(myArray), 'id')
_(ko.toJS(myArray)).pluck('id)