-3

I want to create a nested array in Javascript and want to group data with same property. I have large number of individual array as given below:

var v=['d','c',['c','e','g','h']];
var w=['c','d',['d','e','f']];
var x=['a','b',['b','c','d']];
var y=['b','a',['a','c','e']];
var u=['e','f',['b','c','e']];
var t=['a','c',['b','c','e','f','g']];        

I have tried the following

var z=[];
var a=[];

z.push([t]);
z.push([u]);
z.push([v]);
z.push([w]);    
z.push([x]);
z.push([y]);


var k,j;
for(j=0;j<6;j++){
    for(k=j+1;k<6;k++){
        if(z[j][0]==z[k][1]&& z[j][1]==z[k][0])            
        a.push([z[j][0], z[j][1], z[j][2],z[k][2]]);
     } 
  }

I couldnot access z array in the way i have used? So how can i get the result in array a?

Expected Output (data in array a)

[['d','c',['c','e','g','h'],['d','e','f']],['a','b',['b','c','d'],['a','c','e']]]

I am new to javascript and i am sorry if i am asking a wrong question. I need array a to return.

Here i want to group data in a array, to those pair which have match i.e 1st array 0 index value with 2nd array 1 index value and 1st array 1 index value with 2nd array 0 index value , and grouped these mathcing in a array and push to array a.

In above expected output, array v is matched with array u so i only want to take value v[0],v[1],v[2] and u[2] in a group and ignore u[0] and u[1]. And push the group to array a.

So that i can return the array a in my program

bikas
  • 81
  • 1
  • 8
  • 2
    Missing comma between `['c','e','g','h']['d','e','f']`? – guest271314 Sep 21 '16 at 01:20
  • 1
    You still want `z = [v, w, y, z]` instead of `z = [[v], [w], [y], [z]]`! – Bergi Sep 21 '16 at 01:25
  • @Bergi, Here i want to group data in a array, to those pair which have match i.e 1st array 0 index value with 2nd array 1 index value and 1st array 1 index value with 2nd array 0 index value , and grouped these mathcing in a single and push to array a. So that i can return the array a in my program. – bikas Sep 21 '16 at 01:38
  • 1
    @bikas I'm not talking about `a`, it's the `z` that you are iterating over! You should make an array from your variables, not an array of arrays. `push` takes plain values. – Bergi Sep 21 '16 at 02:13
  • @Bergi oh.i am sorry.i think there is my mistake, i should use `z = [v, w, y, z]` , i will try – bikas Sep 21 '16 at 02:21
  • @bikas yes, please do that, and then accept joey's answer :-) – Bergi Sep 21 '16 at 02:23

2 Answers2

2

Your arrays are already arrays, you don't need to put the '[]' around them when you push them

Joey Wood
  • 273
  • 1
  • 10
  • i want group the match in a array and push that to a array since i need to return array containing array – bikas Sep 21 '16 at 01:55
0

Try concat():

z.push(v.concat(w)).push(x.concat(y));// duplicates will be present.

Then use the method shown in this post to remove duplicates.

Community
  • 1
  • 1
  • i want to scan array z otherwise how could i know v & w and x&y have matching.....i have only given sample array t,u,v,w,x,y. I have such large number of array and need to scan – bikas Sep 21 '16 at 02:03