3

Here have a array set:

totalarray =[
             [1,2,3,4],
             [8,9,10],     
             [15,16,17],
             [8,14,20]
            ]

And I need to make it combine if which set have a same number.

like that:

totalarray =[
             [1,2,3,4],
             [8,9,10,14,20],
             [15,16,17]
            ]

Other example:

totalarray =[
[1,2,3,4],
[6,10,19],
[6,16,4],
[4,14,20]
]

to

totalarray =[
[1,2,3,4,6,10,14,16,19,20]
]

So, I need to make it if any number match on other array and make it to together. e.g:

Array = [[1,2,3,4],[8,9,10],[8,11,12]]; 

Array[1][0] and Array[2][0] is match, so Array will become Array = [1,2,3,4],[8,9,10,11,12].

Any suggestion?

6
  • 2
    I'm not sure I understand your question. You mean which has a repeated number ? Can you explain more please? Commented Aug 10, 2013 at 7:04
  • Sorry, I need to make it if any number match on other array and make it to together. e.g: Array = [1,2,3,4],[8,9,10],[8,11,12]; Array[1][0] and Array[2][0] is match, so Array will become Array = [1,2,3,4],[8,9,10,11,12]. Thanks for fast reply. Commented Aug 10, 2013 at 7:11
  • so you want to join only the arrays that have a value in common? are you sure there's no better way to approach this? merge all arrays and remove duplicates for example? Commented Aug 10, 2013 at 7:17
  • 1
    Your second example seems wrong : there's no common value between the arrays 0+3 and the arrays 1+2. Commented Aug 10, 2013 at 7:26
  • How Array[1][0] and Array[2][0] can match?! Commented Aug 10, 2013 at 7:30

2 Answers 2

1

You have to write the boring looping code. But you might make it a little more manageable with

  • [].push.apply(arr1, arr2); : it pushes all elements of arr2 to arr1 without building a new array
  • indexOf : it looks for an element in an array. If you want to support IE8, as you tagged the question , then you may use $.inArray

Here's the code :

var totalarray =[
  [1,2,3,4],
  [8,9,10],     
  [15,16,17],
  [8,14,20]
];
var result = [totalarray[0]];
function prec(tai) {
  for (var j=0; j<result.length; j++) {
    for (var k=0; k<tai.length; k++) {
      if (result[j].indexOf(tai[k])!=-1) {
        return result[j];
      }
    }
  }
  return null;
}
for (var i=1; i<totalarray.length; i++) {
  var arr = prec(totalarray[i]);
  if (arr) [].push.apply(arr, totalarray[i]);
  else result.push(totalarray[i]);
}

result is the array you want.

Demonstration

Sign up to request clarification or add additional context in comments.

Comments

0

If i understood correctly you are trying to get union

You use the library underscore you can write like this

_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
=> [1, 2, 3, 101, 10]

Link: Underscore.js

1 Comment

Well, that really doesn't look like what OP asks for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.