2012-04-04 11 views

Antwort

3

Here's a sample mit Chaining

//say you have these arrays 
var test1 = [{ 
    "Country": "Spain", 
    "info info1": 0.329235716, 
    "info info2": 0.447683684, 
    "info info3": 0.447683747}, 
{ 
    "Country": "Chile", 
    "info info1": 1.302673893, 
    "info info2": 1.357820775, 
    "info info3": 1.35626442}, 
{ 
    "Country": "USA", 
    "info info1": 7.78805016, 
    "info info2": 26.59681951, 
    "info info3": 9.200900779}]; 

var test2 = [{ 
    "Country": "Germany", 
    "info info1": 0.329235716, 
    "info info2": 0.447683684, 
    "info info3": 0.447683747}, 
{ 
    "Country": "China", 
    "info info1": 1.302673893, 
    "info info2": 1.357820775, 
    "info info3": 1.35626442}, 
{ 
    "Country": "France", 
    "info info1": 7.78805016, 
    "info info2": 26.59681951, 
    "info info3": 9.200900779}]; 


//similar to jQuery, wrap them in an object 
function $W(param) { 
    var obj = {}; 

    //set data 
    obj.data = param; 

    //augment the object with a remove function 
    obj.remove = function(key, val) { 
     var i = 0; 
     //loop through data 
     while (this.data[i]) { 
      if (this.data[i][key] === val) { 
       //if we have that data, splice it 
       //splice changes the array length so we don't increment 
       this.data.splice(i, 1); 
      } else { 
       //else move on to the next item 
       i++; 
      } 
     } 
     //be sure to return the object so that the chain continues 
     return this; 
    } 

    //return object for operation 
    return obj 
} 

//the following will look strangely similar to jQuery 

//remove chile, then USA 
var result1 = $W(test1).remove('Country', 'Chile').remove('Country', 'USA'); 

//remove germany and china 
var result2 = $W(test2).remove('Country', 'China').remove('Country', 'Germany'); 

//should only have spain and france 
$('#test2').val(JSON.stringify(result1)+JSON.stringify(result2));​ 
+0

Wie ist die Funktion mit mehreren auf diese Weise wiederverwendbar? – S16

+0

Die Daten werden in das Objekt eingeschlossen. Eine Funktion wurde hinzugefügt, um bestimmte Daten abzurufen und zu entfernen. Die Verkettung funktioniert, wenn der vorherige Funktionsaufruf das gleiche Objekt zurückgibt, an dem er gearbeitet hat. Dies ermöglicht es dem nächsten Funktionsaufruf, es, seine Methoden und seine Daten zu verwenden. Grundsätzlich funktioniert jQuery auch so. Es sammelt nur Elemente in einem Array, umschließt sie dann in einem Objekt und erweitert sie mit jQuery-Funktionen, die das Objekt nach jeder Operation zurückgeben. – Joseph

+0

Was ist, wenn ich diese Funktion für mehrere Datensätze auf derselben Seite verwenden möchte? – S16

6
+9

w3schools, nicht wahr? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice – Jordan

+1

w3schools ist das erste in Google;) – galchen

+0

@Jordan: Ich sehe keinen Vorteil in Ihrem Kommentar. Wenn Sie auf eine qualifizierte Ressource verweisen möchten, tun Sie es, und die Ressource wäre nicht MDN, es wäre ECMAScript: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262 .pdf –

Verwandte Themen