[英]How to Delete an item from an array with specific string matching
我希望能夠匹配一個特定的字符串(完全匹配而不是部分匹配),然后在匹配時從數組中刪除該特定項目。
我有一些代碼,但它似乎並沒有從數組中刪除該項目。 我確實希望它更改原始數組而不是創建新數組,所以我沒有使用過濾器。
我怎樣才能 go 完成這個?
當前代碼:
let recentSearches = [ { name: "Chicago, IL" }, { name: "Orlando, FL" }, { name: "Dallas, TX" } ]; let stringToRemove = "Dallas, TX"; recentSearches.some(recent => { if (recent.name === stringToRemove) { const index = recentSearches.indexOf(stringToRemove); if (index.== -1) { //Never goes into this if recentSearches,splice(index; 1). console;log(recentSearches); } } }). console;log(recentSearches);
JS Fiddle:在此處輸入鏈接描述
如果您不介意輸出是不同的數組,請使用filter
:
const filteredSearches = recentSearches.filter((recent) => recent.name !== stringToRemove);
如果您需要就地修改數組,您應該以相反的順序訪問元素(在多個匹配的情況下,這會導致索引移動),如下所示:
for (let i = recentSearches.length-1; i >= 0; i--) {
if (recentSearches[i].name === stringToRemove) {
recentSearches.splice(i, 1);
}
}
您的代碼的問題是您使用了recentSearches.indexOf
,但recentSearches
不是字符串數組,因此沒有任何匹配項。 您可以按如下方式修改您的代碼,但在多個數學運算的情況下它將無法正常工作:
recentSearches.forEach((recent, index) => {
if (recent.name === stringToRemove) {
recentSearches.splice(index, 1);
}
});
或者,您可以使用findIndex
(如其他評論和答案中所建議),如下所示:
let index;
while (0 <= (index = recentSearches.findIndex((recent) => recent.name === stringToRemove)) {
recentSearches.splice(index, 1);
}
indexOf()
用於查找完全匹配。 由於您的數組包含對象,因此它們永遠不會等於stringToRemove
。
使用findIndex()
使用比較name
屬性的函數獲取數組元素的索引。
也不需要使用some()
。
let recentSearches = [{ name: "Chicago, IL" }, { name: "Orlando, FL" }, { name: "Dallas, TX" } ]; let stringToRemove = "Dallas, TX"; const index = recentSearches.findIndex(({ name }) => name == stringToRemove); if (index !== -1) { //Never goes into this if recentSearches.splice(index, 1); } console.log(recentSearches);
JSON 搜索做錯了。
我已經添加了完美的代碼來完成您的要求。 查找所有實例並使用 while 循環刪除它們。 這將確保也刪除重復的搜索詞(如果有)。
let recentSearches = [
{name: "Chicago, IL"},
{name: "Orlando, FL"},
{name: "Dallas, TX"}
];
let stringToRemove = "Dallas, TX";
while (recentSearches.findIndex(search => search.name === stringToRemove) > -1) {
const index = recentSearches.findIndex(search => search.name === stringToRemove);
recentSearches.splice(index, 1);
}
console.log(recentSearches);
您可以使用 findindex。 將其存儲在變量中。 並使用拼接
findIndex 的另一個版本,而不是使用 while,您可以使用 for,這里的一個小優勢是索引然后在 for 內部局部范圍內,如果有一個 while 循環,你有索引的額外范圍,你可以關閉通過執行{ let index; while() {..}}
let 的范圍 { let index; while() {..}}
但 for 循環在不使用{}
情況下避免了這種情況。
let recentSearches = [ {name: "Chicago, IL"}, {name: "Orlando, FL"}, {name: "Dallas, TX"} ]; let stringToRemove = "Dallas, TX"; for (let index; index = recentSearches.findIndex( search => search.name === stringToRemove), index > -1;) recentSearches.splice(index, 1); console.log(recentSearches);
您可以使用此代碼:
Array.prototype._arraycopy = function(src, srcPos, dest, destPos, length) {
while ((--length) >= 0) {
dest[destPos++] = src[srcPos++];
}
};
Array.prototype._fastRemove = function(es, i) {
let newSize;
if ((newSize = this.length - 1) > i)
this._arraycopy(es, i + 1, es, i, newSize - i);
es[this.length = newSize] = null;
this.length = newSize;
}
Array.prototype.__removeAt = function(index) {
// Objects.checkIndex(index, size);
const es = this;
const oldValue =es[index];
this._fastRemove(es, index);
return oldValue;
}
Array.prototype.__removeAtValue = function(o) {
const es = this;
const size = this.size;
let i = 0;
(function() {
if (o == null) {
for (; i < size; i++)
if (es[i] == null)
return true;
} else {
for (; i < size; i++)
if (Object.is(o, es[i]))
return true;
}
return false;
})()
this._fastRemove(es, i);
return true;
}
Array.prototype.remove = function(index) {
return this.__removeAt(index)
}
Array.prototype.removeObj = function(obj) {
return this.__removeAtValue(obj);
}
const arr = [1, 3, 4, 5, 10];
console.log(arr);
const rem = arr.remove(1)
console.log({ arr, rem });
const objs = [{ id: 1, name: "Hello" }, { id: 2, name: "Arrow" }, { id: 3, name: "Star" }]
console.log(objs);
const deleted = objs.removeObj({ id: 2, name: "Arrow" });
console.log({ objs, deleted })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.