Javascript 从包含类对象的数组中删除特定项 我的问题
我试图从我的数组中删除一个特定的项,但是,我的数组包含我无法获得句柄的其他对象 我这样定义一个“站”:Javascript 从包含类对象的数组中删除特定项 我的问题,javascript,arrays,knockout.js,Javascript,Arrays,Knockout.js,我试图从我的数组中删除一个特定的项,但是,我的数组包含我无法获得句柄的其他对象 我这样定义一个“站”: /* CLASS Station * @param id int unique id this station * @param name str name of the station * @param location obj the position of the station * in the wo
/* CLASS Station
* @param id int unique id this station
* @param name str name of the station
* @param location obj the position of the station
* in the workflow diagram
*/
var Station = function(id, name, posX=null, posY=null) {
this.id = ko.observable(id || self.getUniqueId());
this.name = ko.observable(name);
this.posX = ko.observable(posX);
this.posY = ko.observable(posY);
};
所以我使用这个函数在数组中添加了一个站
.
.
self.addStation(new Station(76, "Receiving", 0, 10));
现在,我想知道如何通过传递名称从数组中删除,如中所示:
self.removeStation("Receiving");
我想不出来。我已经研究了这里的所有链接,没有运气
全部源代码
谢谢你的关注。
Johnu可以使用函数查找其索引,如下所示:
function arrayFirstIndexOf(array, predicate, predicateOwner) {
for (var i = 0, j = array.length; i < j; i++) {
if (predicate.call(predicateOwner, array[i])) {
return i;
}
}
return -1;
}
var Station = function(id, name, posX=null, posY=null) {
this.id = ko.observable(id || self.getUniqueId()); //observalbe declared
this.name = ko.observable(name);//observalbe declared
this.posX = ko.observable(posX);//observalbe declared
this.posY = ko.observable(posY);//observalbe declared
};
希望这有帮助 u可以使用函数查找其索引,如下所示:
function arrayFirstIndexOf(array, predicate, predicateOwner) {
for (var i = 0, j = array.length; i < j; i++) {
if (predicate.call(predicateOwner, array[i])) {
return i;
}
}
return -1;
}
var Station = function(id, name, posX=null, posY=null) {
this.id = ko.observable(id || self.getUniqueId()); //observalbe declared
this.name = ko.observable(name);//observalbe declared
this.posX = ko.observable(posX);//observalbe declared
this.posY = ko.observable(posY);//observalbe declared
};
希望这有帮助 感谢您的回复。但通过使用以下代码,我找到了一个可接受的问题解决方案:
/* Remove Station */
self.removeStation = function (Name) {
var c = -1;
ko.utils.arrayForEach(this.station(), function(item) {
c++;
var value = item.name();
if(value==Name){
console.log("found it! at index " + c);
self.station().splice(c,1);
}
});
// Force the UI to update
self.station.valueHasMutated();
}
我意识到这不是最干净或最有效的解决方案,但它似乎有效。我很想知道如何优化这一点,但这超出了我的工资等级,哈哈。谢谢你的回复。但通过使用以下代码,我找到了一个可接受的问题解决方案:
/* Remove Station */
self.removeStation = function (Name) {
var c = -1;
ko.utils.arrayForEach(this.station(), function(item) {
c++;
var value = item.name();
if(value==Name){
console.log("found it! at index " + c);
self.station().splice(c,1);
}
});
// Force the UI to update
self.station.valueHasMutated();
}
我意识到这不是最干净或最有效的解决方案,但它似乎有效。我很想知道如何优化这一点,但这超出了我的工资等级,哈哈。sry对于长时间的回答,我认为你遇到的问题是关于你声明的变量和你使用的变量不是同一类型的,当你分配给你的对象时,你没有使用任何转换,如下所示:
function arrayFirstIndexOf(array, predicate, predicateOwner) {
for (var i = 0, j = array.length; i < j; i++) {
if (predicate.call(predicateOwner, array[i])) {
return i;
}
}
return -1;
}
var Station = function(id, name, posX=null, posY=null) {
this.id = ko.observable(id || self.getUniqueId()); //observalbe declared
this.name = ko.observable(name);//observalbe declared
this.posX = ko.observable(posX);//observalbe declared
this.posY = ko.observable(posY);//observalbe declared
};
但是你用的是
var newObj = {
id: "82",
name: "Updated Name",
posX: 92,
posY: 88
}
self.modifyStation("name","Shipping",newObj);
分配时:
objNewItem.id = oldId; // Put old ID back in
self.station.push(objNewItem); //not convert its properties to observable will make some errors on binding html.
我用你的例子做了一个有用的例子,请阅读它,当你不明白哪一部分时,请回答。希望这有帮助
函数arrayFirstIndexOf(数组、谓词、谓词所有者){
对于(var i=0,j=array.length;i-1和自站拼接(索引,1);
}
/*更新站点**需要修复**
*
*@param findBy要查找的字符串属性(“id”或“name”)
*@param cmpVal要与之比较的字符串值
*@param objneItem对象新对象替换旧对象
* */
self.modifyStation=函数(findBy、cmpVal、objneItem){
var来源指数;
奥尔迪德变种;
var=false;
/*查找旧站点的索引*/
var c=-1;
ko.utils.arrayForEach(this.station(),函数(项){
C++;
开关(findBy){
案例“id”:
var值=ko.展开(项目id);
如果(值==cmpVal){
sourceIndex=c;
oldId=值;
发现=真;
}
打破
案例“名称”:
var值=ko.展开(项目名称);
如果(值==cmpVal){
sourceIndex=c;
oldId=ko.展开(项目id);
发现=真;
}
打破
}
});
/*除去O