0

I have a list of contacts from two arrays. Some of the elements maybe the same, but I need to know which array has unique values and keep those unique values in that array. In other words, I want to track the unique values from which array they came from. This is my spaghetti code that I have so far:

for (var i = 0; i < emailNews.length; i++)
        {
            for (var j = 0; j < emailHealth.length; j++) 
                {
                    if (emailNews[i] == emailHealth[j]) {
                        $('#userList').append(descriptionNews[i] + '<td><span style="margin-right: 5px;">Advanced</span><input type="checkbox" name="advanced" id="advancedCheck" checked></td><td><button type="button" class="btn btn-danger btn-sm btn-user-delete" id="' + userLogin + '" >Delete</button></td></tr>');
                   } else {
                       console.log(descriptionHealth[j]);
                       $("#userList").append(descriptionHealth[j] + '<td><button type="button" class="btn btn-danger btn-sm" id="deleteEnd">Delete</button></td></tr>');
                    }
                }

        }
5
  • Is requirement to remove duplicate elements from an array? Commented Sep 29, 2017 at 19:15
  • yes, but I need to know which arrays contains which unique ones Commented Sep 29, 2017 at 19:17
  • Not sure what you mean by "which arrays contains which unique ones"? Commented Sep 29, 2017 at 19:19
  • both arrays are from two lists of emails. I need to print to HTML if the specific user is contained in both lists. If not, then which list is the differing value coming from Commented Sep 29, 2017 at 19:21
  • please add some data and what you like to achieve. Commented Sep 29, 2017 at 19:37

2 Answers 2

1

Your description is a little confusing, but as I understand, you have two arrays, and you wish to know the unique items in each array (items not in the other array).

So given arrays a and b, you could create two new arrays that hold just the unique ones.

// unique--v---------------------v
var a = ["foo", "bar", "baz", "zing"];

// unique--v
var b = ["oof", "bar", "baz"];

var unique_a = a.filter(item => !b.includes(item));
var unique_b = b.filter(item => !a.includes(item));

console.log("Unique in `a`: %s", JSON.stringify(unique_a));
console.log("Unique in `b`: %s", JSON.stringify(unique_b));

So now that you have your unique values, and you know which array they came from, you can do what you need with those values.


If you want to do it while you're looping, that's just as easy. Just take the other array and call .includes() on it with the current item.

a.forEach(item => {
  if (b.includes(item)) {
    // not unique...
  } else {
    // unique
  }
});

If you need to work with all items, separating out the unique ones both ways, then also working with the shared ones, make a Set of all, and iterate that.

for (const item of new Set([...a, ...b])) {
  var in_a = a.includes(item);
  var in_b = b.includes(item);
  if (in_a && in_b) {
    // not unique
  } else if (in_a) {
    // unique to 'a'
  } else {
    // unique to 'b'
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If requirement is to remove duplicate elements from one or more arrays you can use $.unique(). Set arrays as properties of objects and include a property which uniquely identifies the current object

(() => {
  let arr1 = {
    arr: [2, 2, 3],
    name: "arr1"
  };

  let arr2 = {
    arr: [1, 1, 2, 3, 3],
    name: "arr2"
  };

  let res;

  let user = 1;

  if (arr1.arr.indexOf(user) > -1 && arr2.arr.indexOf(user) > -1) {

    res = $.unique($.map([arr1.arr, arr2.arr], arr => arr))

  } else {
    for (let o of Object.values([arr1, arr2])) {
      if (!o.arr.indexOf(user) > -1) {
        res = o.name + " does not contain " + user;
        break;
      }
    }
  }

  console.log(res);

})();

(() => {
  let arr1 = {
    arr: [1, 2, 2, 3],
    name: "arr1"
  };

  let arr2 = {
    arr: [1, 1, 2, 3, 3],
    name: "arr2"
  };

  let res;

  let user = 1;

  if (arr1.arr.indexOf(user) > -1 && arr2.arr.indexOf(user) > -1) {
    res = $.unique($.map([arr1.arr, arr2.arr], arr => arr))

  } else {
    for (let o of Object.values([arr1, arr2])) {
      if (!o.arr.indexOf(user) > -1) {
        res = o.name + " does not contain " + user;
        break;
      }
    }
  }

  console.log(res);

})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>

Comments

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.