1

I have two simple arrays:

var arr_must_exist = ["1","2","3"];
var arr_created    = ["2","4","7","8"]; //This is varies depend of the array created.

So, if arr_created is like the example there should be an alert like Your choice must contain 1 and 3

Example:

arr_created    = ["2","4","7","8"];
alert(`Your choice must contain 1 and 3`)

arr_created    = ["2","3","7","8"];
alert(`Your choice must contain 1`)

arr_created    = ["4","5","7","8"];
alert(`Your choice must contain 1, 2 and 3`)

arr_created    = ["1","2","9","3"];
alert(`Your choice is valid`)

I use $.each but it's only check the array. Not comparing them.

$.each(arr_is_must_exist , function(index, val_must_exist) { 

        $.each(arr_is_exist_in_table , function(index, val_is_exist) { 
            if(val_must_exist != val_is_exist){
              alert("Your choice must contain 1,2,3");
            }
        });
});

I look here but there is not accepted answer. I use IE and mozilla (it's for local user)

Vahn
  • 542
  • 1
  • 10
  • 25
  • Possible duplicate of [What is the fastest or most elegant way to compute a set difference using Javascript arrays?](https://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javasc) – Kevin Ji Dec 07 '17 at 04:26

6 Answers6

9

You can find the array of missing items like below:

var arr_must_exist = ["1","2","3"];
var arr_created    = ["2","4","7","8"];
var missing = arr_must_exist.filter(e => arr_created.indexOf(e) < 0);

console.log(missing);
Daniel Tran
  • 6,083
  • 12
  • 25
2

You can do by simple javascript with your log message

var arr_must_exist = ["1","2","3"];
var arr_created    = ["2","4","7","8"]; 
//This is varies depend of the array created.

var numberNotAvailable = [];

for(var i = 0; i<arr_must_exist.length; i++){
  console.log(arr_created.indexOf(arr_must_exist[i]));
  if(arr_created.indexOf(arr_must_exist[i]) < 0){
        console.log(arr_must_exist[i] + ' added');    
        numberNotAvailable.push(arr_must_exist[i])
  }  
}

var logMessage ;
if(numberNotAvailable.length == 0){
  logMessage = 'Your choice is valid';
}
else if(numberNotAvailable.length == 1){
  logMessage = 'Your choice must contain ' + numberNotAvailable[0];  
}
else if(numberNotAvailable.length == 2){
  logMessage = 'Your choice must contain ' + numberNotAvailable[0] + ' and ' + numberNotAvailable[1];  
}
else if(numberNotAvailable.length > 2){  
  logMessage = 'Your choice must contain ' + fruits.join();  
}

console.log(logMessage);
programtreasures
  • 4,250
  • 1
  • 10
  • 29
1

It can be done in javascript using forEach & join array methods

var arr_must_exist = ["1", "2", "3"];
var arr_created = ["2", "4", "7", "8"];
  
// An array to store the elements which are not in arr_created
var missingElements = [];

// looping over arr_must_exist and checking if 
// element is present in arr_created using indexOf
arr_must_exist.forEach(function(item) {
  if (arr_created.indexOf(item) === -1) {
    // if not push the element in the array
    missingElements.push(item)

  }
})
   
// if missingElements length is greater than 1 mean if it contain 
// any element then join all the element ,create string with a comma 
//separator
if (missingElements.length > 0) {
  alert(missingElements.join(','))
}
brk
  • 48,835
  • 10
  • 56
  • 78
0

You will have to write a function that does the comparing and stores the difference in an array

var arr_must_exist = ["1","2","3"];
var arr_created    = ["2","4","7","8"];
var diff = [];
arr_must_exist.forEach(function(value){
   if(arr_created.indexOf(value) < 0) {
     diff.push(value);
    } 
});
var str = '';
if(diff.length) {
    var str = 'Your choice must contain ';
    diff.forEach(function(value, index) {
        str += ((index+1 == diff.length) ? ' and ' + value : value)
    });
} else {
    str = 'Your choice is valid';
}
alert(str);
cdoshi
  • 2,772
  • 1
  • 13
  • 20
0

This uses a Set to check for the existence of each item in arr_must_exist.

It also uses a regex to get the "and" or the "," right, and a template literal to put the message together.

const arr_must_exist = ["1","2","3"];

function getMsg(arr_created) {
  const set = new Set(arr_created);
  const m = arr_must_exist.filter(s => !set.has(s))
                          .join(", ")
                          .replace(/, ([^,]+)$/, " and $1");

  return `Your choice ${m ? "must contain " + m : "is valid"}`;
}

console.log(getMsg(["2","4","7","8"]));
console.log(getMsg(["2","3","7","8"]));
console.log(getMsg(["4","5","7","8"]));
console.log(getMsg(["1","2","9","3"]));
0

There various way to doing so the set of limited number of practice may you use. Like

  1. By $.map() function
  2. By $.filter() function
  3. By array compare with multiple for Loops

Code Examples :

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var arr_must_exist = ["1","2","3"];
var arr_created    = ["2","4","7","8"];
var noElement =[];
// This Example contains the `$.map()` function to accomplish the task.
$.map(arr_must_exist, function(el){
  if($.inArray(el, arr_created) < 0)
  {
        noElement.push(el);
       return null;
  }
  else
  {
      return el;
  } 
  return $.inArray(el, arr_created) < 0 ? null : el;
});
if(noElement.length <= 0)
{
console.log("Your choice is valid");
}
else
{
console.log("Your choice must contain "+noElement);
}

//This example contain the `$.filter()` check;

$checkarray = arr_must_exist.filter(e => arr_created.indexOf(e) < 0);


 if($checkarray.length == 0)
 {
    console.log("Your choice is valid");
 }
 else
 {
    console.log("Your choice must contain "+ $checkarray);
 }
</script>
A.D.
  • 2,352
  • 2
  • 15
  • 25