[英]Array.push return pushed value?
為什么修改Array.push()
以返回推送的對象而不是新數組的長度可能是一個壞主意,是否有任何實質性的原因?
我不知道這是否已經被提議或詢問過; 谷歌搜索只返回了無數與Array.push()
的當前功能相關的問題。
這是此功能的示例實現,請隨時更正:
;(function() {
var _push = Array.prototype.push;
Array.prototype.push = function() {
return this[_push.apply(this, arguments) - 1];
}
}());
然后,您將能夠執行以下操作:
var someArray = [],
value = "hello world";
function someFunction(value, obj) {
obj["someKey"] = value;
}
someFunction(value, someArray.push({}));
例如,其中someFunction
修改作為第二個參數傳入的對象。 現在someArray
的內容是[{"someKey": "hello world"}]
。
這種方法有什么缺點嗎?
TLDR;
當您改為使用array.concat[]
添加元素時,您可以獲得變異數組的返回值。
concat
是一種將兩個數組“添加”或“連接”在一起的方法。 這個方法很棒的地方在於它有一個結果數組的返回值,所以它可以被鏈接起來。
newArray = oldArray.concat[newItem];
這也允許您將功能鏈接在一起
updatedArray = oldArray.filter((item) => { item.id !== updatedItem.id).concat[updatedItem]};
其中item = {id: someID, value: someUpdatedValue}
需要注意的主要事情是,您需要將數組傳遞給concat
。
因此,請確保將要“推”的值放在幾個方括號內,這樣就可以了。
這將為您提供push()
所期望的功能
您可以使用+
運算符將兩個數組“添加”在一起,或者將要連接的數組作為參數傳遞給concat()
。
let arrayAB = arrayA + arrayB;
let arrayCD = concat(arrayC, arrayD);
請注意,通過使用concat
方法,您可以利用concat
之前和之后的“鏈接”命令。
為什么修改
Array.push()
以返回推送的對象而不是新數組的長度可能是一個壞主意,是否有任何實質性的原因?
當然有一個:其他代碼將期望Array::push
的行為符合規范中的定義,即返回新的長度。 如果您確實重新定義了內置函數以使其行為異常,其他開發人員會發現您的代碼難以理解。
至少為該方法選擇一個不同的名稱。
然后,您將能夠執行以下操作:
someFunction(value, someArray.push({}));
呃,什么? 是的,我的第二點已經發生了:-)
但是,即使您沒有使用push
這也不會達到您想要做的事情。 您應該表達的組合是“將由鍵和值組成的對象添加到數組中”。 用更函數式的風格,讓someFunction
返回這個對象,就可以寫了
var someArray = [],
value = "hello world";
function someFunction(value, obj) {
obj["someKey"] = value;
return obj;
}
someArray.push(someFunction(value, {}));
正如一個歷史記錄——有一個舊版本的 JavaScript—— JavaScript version 1.2
——它處理許多數組函數的方式完全不同。
特別是對於這個問題, Array.push
確實返回了項目,而不是數組的長度。
也就是說, 1.2
已經幾十年沒有使用了——但一些非常古老的參考資料可能仍然提到這種行為。
隨着ES6的到來,建議以適當的方式擴展數組類,然后重寫push
方法:
class XArray extends Array { push() { super.push(...arguments); return (arguments.length === 1) ? arguments[0] : arguments; } } //---- Application let list = [1, 3, 7,5]; list = new XArray(...list); console.log( 'Push one item : ',list.push(4) ); console.log( 'Push multi-items :', list.push(-9, 2) ); console.log( 'Check length :' , list.length )
var arr = [];
var element = Math.random();
assert(element === arr[arr.push(element)-1]);
方法push()返回最后添加的元素,這使得在創建短函數/reducer 時非常不方便。 此外, push() - 在 JS 中是一個相當古老的東西。 另一方面,我們有傳播運算符[...] , 它更快並且可以滿足您的需求:它完全返回一個數組。
// to concat arrays
const a = [1,2,3];
const b = [...a, 4, 5];
console.log(b) // [1, 2, 3, 4, 5];
// to concat and get a length
const arrA = [1,2,3,4,5];
const arrB = [6,7,8];
console.log([0, ...arrA, ...arrB, 9].length); // 10
// to reduce
const arr = ["red", "green", "blue"];
const liArr = arr.reduce( (acc,cur) => [...acc, `<li style='color:${cur}'>${cur}</li>`],[]);
console.log(liArr);
//[ "<li style='color:red'>red</li>",
//"<li style='color:green'>green</li>",
//"<li style='color:blue'>blue</li>" ]
用 someArray[someArray.length]={}代替 someArray.push({}) 怎么樣? 賦值的值是被賦值的值。
var someArray = [], value = "hello world"; function someFunction(value, obj) { obj["someKey"] = value; } someFunction(value, someArray[someArray.length]={}); console.log(someArray)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.