Asked  7 Months ago    Answers:  5   Viewed   33 times

I have 2 arrays:

var a = [1, 2, 3]
var b = [a, b, c]

What I want to get as a result is:

[[1, a], [2, b], [3, c]]

It seems simple but I just can't figure out.

I want the result to be one array with each of the elements from the two arrays zipped together.

 Answers

63

Use the map method:

var a = [1, 2, 3]
var b = ['a', 'b', 'c']

var c = a.map(function(e, i) {
  return [e, b[i]];
});

console.log(c)

DEMO

Tuesday, June 1, 2021
 
francadaval
answered 7 Months ago
77
var z = new int[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);
Friday, June 4, 2021
 
Eddas
answered 6 Months ago
63

Here's complete code:

StringIntTuple.java:

public class StringIntTuple{
    public final int intValue;
    public final String stringValue;
    public StringIntTuple(int intValue, String stringValue){
        this.intValue = intValue;
        this.stringValue = stringValue;
    }
    public String toString(){
        return "(" + this.intValue + ", " + this.stringValue + ")";
    }

}

StringIntTupleStringComparator.java:

import java.util.Comparator;


public class StringIntTupleStringComparator implements
        Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a, StringIntTuple b) {
        // TODO Auto-generated method stub
        return a.stringValue.compareTo(b.stringValue);
    }

}

StringIntTupleIntComparator.java:

import java.util.Comparator;


public class StringIntTupleIntComparator implements Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a,
            StringIntTuple b) {
        return ((Integer)a.intValue).compareTo((Integer)b.intValue);
    }

}

Driver.java:

import java.util.ArrayList;
import java.util.Collections;


public class Driver {

    /**
     * @param args
     */
    public static String[] names = new String[] {"Monkey1", "Dog2", "Horse3", "Cow4", "Spider5"};
    public static int[] data = new int[] {1,2,3,4,5};
    public static void main(String[] args) {
        ArrayList<StringIntTuple> list = new ArrayList<StringIntTuple>();
        for(int i =0; i<names.length; i++){
            list.add(new StringIntTuple(data[i],names[i]));
        }
        Collections.sort(list, new StringIntTupleIntComparator());
        System.out.println(list.toString());
        Collections.sort(list, new StringIntTupleStringComparator());
        System.out.println(list.toString());
    }


}

Output (sorted first by int field, then by String field):

[(1, Monkey1), (2, Dog2), (3, Horse3), (4, Cow4), (5, Spider5)]

[(4, Cow4), (2, Dog2), (3, Horse3), (1, Monkey1), (5, Spider5)]

EDIT 1 (extra info):

If you want to make this work for any Tuple, i.e. which doesn't constrain the field types to int, String, you can simply do the same operation with generics, i.e.:

public class Tuple<A,B>{
    public Tuple(A aValue, B bValue){
        this.aValue = aValue;
        this.bValue = bValue;
    }
    public final A aValue;
    public final B bValue;

}

Then, just tweak the Comparators accordingly, and you have a generic solution. EDIT 2(After lunch): Here it is.

public class TupleAComparator<A extends Comparable<A>,B extends Comparable<B>> implements Comparator<Tuple<A,B>> {

    @Override
    public int compare(Tuple<A, B> t1, Tuple<A, B> t2) {
        return t1.aValue.compareTo(t2.aValue);
    }

}

EDIT 3: Code supplement as answer to Comment #1 (augmenting comment #2) TupleArrayList.java:

import java.util.ArrayList;
import java.util.List;


public class TupleArrayList<A,B> extends ArrayList<Tuple<A,B>> {

    /**
     * An ArrayList for tuples that can generate a List of tuples' elements from a specific position within each tuple
     */
    private static final long serialVersionUID = -6931669375802967253L;

    public List<A> GetAValues(){
        ArrayList<A> aArr = new ArrayList<A>(this.size());
        for(Tuple<A,B> tuple : this){
            aArr.add(tuple.aValue);
        }
        return aArr;
    }

    public List<B> GetBValues(){
        ArrayList<B> bArr = new ArrayList<B>(this.size());
        for(Tuple<A,B> tuple : this){
            bArr.add(tuple.bValue);
        }
        return bArr;
    }

}
Wednesday, June 9, 2021
 
Skipper
answered 6 Months ago
85

Usage: isEqArrays(arr1, arr2)

//
// Array comparsion
//

function inArray(array, el) {
  for ( var i = array.length; i--; ) {
    if ( array[i] === el ) return true;
  }
  return false;
}

function isEqArrays(arr1, arr2) {
  if ( arr1.length !== arr2.length ) {
    return false;
  }
  for ( var i = arr1.length; i--; ) {
    if ( !inArray( arr2, arr1[i] ) ) {
      return false;
    }
  }
  return true;
}
Thursday, July 29, 2021
 
maniclorn
answered 4 Months ago
74

There is Array.filter():

var numbers = [1, 2, 3, 4, 5];
var filtered = numbers.filter(function(x) { return x > 3; });

// As a JavaScript 1.8 expression closure
filtered = numbers.filter(function(x) x > 3);

Note that Array.filter() is not standard ECMAScript, and it does not appear in ECMAScript specs older than ES5 (thanks Yi Jiang and jAndy). As such, it may not be supported by other ECMAScript dialects like JScript (on MSIE).

Nov 2020 Update: Array.filter is now supported across all major browsers.

Saturday, September 4, 2021
 
Amumu
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share