Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di freetom
    Registrato dal
    Nov 2001
    Messaggi
    3,725

    Sottrazione di arrays...

    Ho un array PRIMO con i seguenti valori (a,b,c,d) e un'altro array di nome SECONDO con i seguenti valori (c,d)

    Io vorrei poter sottrarre il secondo al primo in modo da ottenere solo gli elementi del primo meno quelli del primo in comune col secondo..

    ovvero dalla "mia sottrazione" (PRIMO-SECONDO) dovrei ottenere:

    un TERZO array avente come valori solo: a,b

    Quale funzione puo' generarmi tale risultato?



    THANKS VERY MUCH!


  2. #2
    Utente di HTML.it L'avatar di arn
    Registrato dal
    Jun 2002
    Messaggi
    420
    una cosa simile?

    Array.arrayDifference
    Author(s): jgarnet@elektronicabcn.com
    Flash version: 6
    DateTime: 20.01.2004
    Array.arrayDifference
    ------------------------
    Array.prototype.arrayDifference = function (arr2) {
    var its = new Array ();
    var diff_arr = new Array ();
    var separator = "###";
    for (var i = 0; i < this.length; i++)
    {
    var it = this[i];
    for (var k = 0; k < arr2.length; k++)
    {
    if (this[i] == arr2[k])
    {
    it = separator;
    break;
    }
    }
    its.push (it);
    }
    for (var i = 0; i < arr2.length; i++)
    {
    var it = arr2[i];
    for (var k = 0; k < this.length; k++)
    {
    if (arr2[i] == this[k])
    {
    it = separator;
    break;
    }
    }
    its.push (it);
    }
    for (var m = 0; m < its.length; m++)
    {
    if (its[m] != separator)
    {
    diff_arr.push (its[m]);
    }
    }
    return (diff_arr);
    };
    ------------------------
    usage:
    ------------------------
    arr_1 = ["one", "two", "three", "one", "one"];
    arr_2 = ["one", "five", "three", "twenty", "eleven"]; trace (arr_1.arrayDifference (arr_2)); trace
    (arr_2.arrayDifference (arr_1));
    La rapidità che è una virtù, genera un vizio che è la fretta !
    -------------------------
    Browser: Opera & Firefox

  3. #3
    Utente di HTML.it L'avatar di arn
    Registrato dal
    Jun 2002
    Messaggi
    420
    e ti pùò tornare anche utile questo:

    Array.in_array
    Author(s): andrea@3site.it
    Flash version: 6
    DateTime: 25.03.2004
    Array.prototype.in_array = function( what ) {
    // andr3a [ 25 / 03 / 2004 ]
    // check if a value is inside an array
    // EXAMPLE:
    //var myArray = new Array( "hello", "world", Array("one", "two") );
    //trace( myArray.in_array( "hello" ) ); // true
    //trace( myArray.in_array( "hi" ) ); // false
    //trace( myArray.in_array( "two" ) ); // true
    for( var a = 0; a < this.length; a++ ) {
    if( this[a] == what ) {
    return true;
    }
    else if( this[a] instanceof Array ) {
    return this[a].in_array( what );
    }
    }
    return false;
    }
    // USAGE:
    var myArray = new Array( "hello", "world", Array("one", "two") );
    trace( myArray.in_array( "hello" ) ); // true
    trace( myArray.in_array( "hi" ) ); // false
    trace( myArray.in_array( "two" ) ); // true
    La rapidità che è una virtù, genera un vizio che è la fretta !
    -------------------------
    Browser: Opera & Firefox

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.