हाँ, Array.map () या $ .map () एक ही काम करता है।
//array.map:
var ids = this.fruits.map(function(v){
return v.Id;
});
//jQuery.map:
var ids2 = $.map(this.fruits, function (v){
return v.Id;
});
console.log(ids, ids2);
http://jsfiddle.net/NsCXJ/1/
चूंकि array.map पुराने ब्राउज़रों में समर्थित नहीं है, इसलिए मेरा सुझाव है कि आप jQuery विधि से चिपके रहें।
यदि आप किसी कारण से दूसरे को पसंद करते हैं तो आप हमेशा पुराने ब्राउज़र समर्थन के लिए पॉलीफ़िल जोड़ सकते हैं।
आप हमेशा ऐरे प्रोटोटाइप में भी कस्टम तरीके जोड़ सकते हैं:
Array.prototype.select = function(expr){
var arr = this;
//do custom stuff
return arr.map(expr); //or $.map(expr);
};
var ids = this.fruits.select(function(v){
return v.Id;
});
एक विस्तारित संस्करण जो फ़ंक्शन कंस्ट्रक्टर का उपयोग करता है यदि आप एक स्ट्रिंग पास करते हैं। शायद साथ में खेलने के लिए कुछ:
Array.prototype.select = function(expr){
var arr = this;
switch(typeof expr){
case 'function':
return $.map(arr, expr);
break;
case 'string':
try{
var func = new Function(expr.split('.')[0],
'return ' + expr + ';');
return $.map(arr, func);
}catch(e){
return null;
}
break;
default:
throw new ReferenceError('expr not defined or not supported');
break;
}
};
console.log(fruits.select('x.Id'));
http://jsfiddle.net/aL85j/
अपडेट करें:
चूंकि यह इतना लोकप्रिय उत्तर बन गया है, मैं इसी तरह के अपने where()
+ जोड़ रहा हूं firstOrDefault()
। इन्हें स्ट्रिंग आधारित फंक्शन कंस्ट्रक्टर एप्रोच (जो सबसे तेज़ है) के साथ भी इस्तेमाल किया जा सकता है, लेकिन यहाँ एक अन्य एप्रोच है जिसका उपयोग ऑब्जेक्ट लिटरेचर फ़िल्टर के रूप में किया जाता है:
Array.prototype.where = function (filter) {
var collection = this;
switch(typeof filter) {
case 'function':
return $.grep(collection, filter);
case 'object':
for(var property in filter) {
if(!filter.hasOwnProperty(property))
continue; // ignore inherited properties
collection = $.grep(collection, function (item) {
return item[property] === filter[property];
});
}
return collection.slice(0); // copy the array
// (in case of empty object filter)
default:
throw new TypeError('func must be either a' +
'function or an object of properties and values to filter by');
}
};
Array.prototype.firstOrDefault = function(func){
return this.where(func)[0] || null;
};
उपयोग:
var persons = [{ name: 'foo', age: 1 }, { name: 'bar', age: 2 }];
// returns an array with one element:
var result1 = persons.where({ age: 1, name: 'foo' });
// returns the first matching item in the array, or null if no match
var result2 = persons.firstOrDefault({ age: 1, name: 'foo' });
फ़ंक्शन कंस्ट्रक्टर बनाम ऑब्जेक्ट शाब्दिक गति की तुलना करने के लिए यहां एक jsperf परीक्षण है। यदि आप पूर्व का उपयोग करने का निर्णय लेते हैं, तो तार को सही ढंग से उद्धृत करने का ध्यान रखें।
मेरी व्यक्तिगत प्राथमिकता 1-2 गुणों को फ़िल्टर करते समय ऑब्जेक्ट शाब्दिक आधारित समाधानों का उपयोग करना है, और अधिक जटिल फ़िल्टरिंग के लिए कॉलबैक फ़ंक्शन पास करना है।
मूल ऑब्जेक्ट प्रोटोटाइप में विधियाँ जोड़ने पर मैं इसे 2 सामान्य युक्तियों के साथ समाप्त करूँगा:
ओवरराइटिंग से पहले मौजूदा तरीकों की घटना के लिए जाँच करें:
if(!Array.prototype.where) {
Array.prototype.where = ...
यदि आपको IE8 और नीचे का समर्थन करने की आवश्यकता नहीं है, तो उन्हें गैर- प्रवर्तनीय बनाने के लिए Object.defineProperty का उपयोग करके विधियों को परिभाषित करें । यदि कोई किसी for..in
सरणी पर उपयोग किया जाता है (जो पहली जगह में गलत है) तो वे गुण के साथ-साथ असंख्य गुणों को पुनरावृत्त करेंगे। बिलकुल चौकन्ना।