1

I need some help. I would like to merge something like this 12,12,12,13,13,14,14,14,15,15 into 12,13,14,15. i have this code so far.

 if ($('#branchName' + branchList.branch).length == 0) {
                    $('#zzz').append(
                        '<span>' + branchList.branch + '</span>' +
                            '<ul id="branchName' + branchList.branch + '">' +
                            '<li>' +
                            '<span id="combo' + branchList.combo + '">' + branchList.combo + '</span>' +
                            '</li>' +
                            '</ul>');
                    if ($('#combo' + branchList.combo).length == 0) {
                        //if exists go to else
                    } else {
                        //append combo
                    }
                }

                else {
                    $('#branchName' + branchList.branch).append(
                        '<li>' +
                            '<span id="combo' + branchList.combo + '">' + branchList.combo + '</span>' +
                            '</li>');           
                }

Update : I already merge the numbers on my if else by appending it on existing one. Now my problem is where should I put again the next if statement for my combo.

John
  • 19
  • 7

2 Answers2

3

Try the filter property. Say that you have those values in an array

arr=[1,1,1,2,3,4,5];
new_arr = arr.filter(function(item, pos) {
    return arr.indexOf(item) === pos;
});

The filter, takes a callback function and calls it on each element of the array, like .map and takes at-most three argument, the value of the element(from your array), the index and the Array itself.

The idea behind this is, you take the one element then check if the indexOf(that_element) is equal to the position of the element.

Walkthrough.

step 1: indexOf(1) = 0 and pos = 0, indexOf(1) === pos // true so the element is pushed in the new_arr step 2: indexOf(1) = 0 and pos = 1, // condition false, so discard.

so on..

This property of allowing and discarding probably comes from duck-typing

Community
  • 1
  • 1
argentum47
  • 2,385
  • 1
  • 18
  • 20
  • You should probably mention you got that answer from __[here](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array/9229821#9229821)__. – Cerbrus Oct 29 '14 at 08:58
  • oh looks like simple.. gonna try this.. ty – John Oct 29 '14 at 08:59
  • yow inside that function is the parameter? (item,pos) – John Oct 29 '14 at 09:43
  • explain what you mean. did you read the doc link? – argentum47 Oct 29 '14 at 10:10
  • i tried this, but that is not the answer... im trying nested if – John Oct 31 '14 at 05:33
  • you need to provide more details then. It didn't work doesnot give much detail. Because the code does remove the duplicate elements. – argentum47 Oct 31 '14 at 06:14
  • @argentum47 ty. I already merge the numbers on my if else by appending it on existing one, not removing it. Now my problem is where should I put again the next if statement for my combo, because there is duplicate again. – John Oct 31 '14 at 06:29
  • @argentum47 my output above Branch1 = Combo1, Combo1, Combo2,Combo2. Branch2 = Combo1, Combo1, Combo2,Combo2 my next output should be as Branch1 = Combo1,Combo2. Branch2= Combo1,Combo2. – John Oct 31 '14 at 06:30
  • @Cerbrus sir, with due respect, there are multiple ways to do this, `filter` is one way , `using a hash` is another way. This problem is so common that any solution will match with another solution. If I write. use `Set` in es6 its going to be copy pasting MDN docs now. – argentum47 Mar 24 '20 at 12:05
  • @John https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array/9229821#9229821 please refer here for all possible approaches – argentum47 Mar 24 '20 at 12:05
  • Dude, this answer is 6 years old... Why are you even replying to it now? – Cerbrus Mar 24 '20 at 12:06
  • @Cerbrus hahahaha.. sorry man. I came back on SO after 4 years. Its been so long I have actually helped and learned by helping. – argentum47 Mar 24 '20 at 12:21
0

You can import a library Underscore
In Underscore, has a simple function to do this:

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