0

Hi guys i have one js function in that i have one array which will get the depotidowner id's it will be like 1,2,4,5,6

So what i am trying is i would like to check the condition like if all the values in array is same then return that value if not return false;

Here is my array:

function createOrderForTicket()
{
    var selectedVehicles = [];
    var selectedVehiclesDepotIds = [];
    $('#vehicleListForNewOrder input:checked').each(function() {
        selectedVehicles.push($(this).val());
        selectedVehiclesDepotIds.attr('depotIdOwner');
    });

    if (0 === selectedVehicles.length) {
        $('#vehicleListForNewOrderError').show();
        $('#vehicleListForNewOrderError').addClass('error');

        return false;
    }

    if (0 === selectedVehiclesDepotIds.length) {
        return false;
    }



    var vehicleIds = selectedVehicles.join(',');
    var contactId = $('#contactId').val();
    var personId = $('#personId').val();
    var ticketId = $('#ticketId').val();
    var depotIdOwner = selectedVehiclesDepotIds.val();
    var url = vbdBaseUrl + '/order/customer/index/contactId/' + contactId + 
        '/personId/' + personId + '/ticketId/' + ticketId + '/vehicleId/' + vehicleIds + '/id/';

    if (depotIdOwner) {
        url = url + '/depotId/' + depotIdOwner;
    }

    window.open(url, '_blank');
    location.reload();
}
suresh
  • 439
  • 3
  • 18
  • Possible duplicate of [Check if all values of array are equal](https://stackoverflow.com/questions/14832603/check-if-all-values-of-array-are-equal) – Zenoo Nov 12 '18 at 09:56
  • A simple loop will do, start off by comparing length, if both lengths are the same then compare each element. – SPlatten Nov 12 '18 at 09:57
  • i didn't understand can you please explain clearly – suresh Nov 12 '18 at 09:59
  • @Zenoo i have checked that link i haven't understand how to use so thats why i have posted another question..can you please explain me brielfy – suresh Nov 12 '18 at 09:59
  • If I understand correctly, you are trying to check if array contains duplicate values, is that right? Could you please post array with example values? – Tornike Shavishvili Nov 12 '18 at 10:03
  • yes correct my example values will be 1,2,3,4,etc.. or it might be 1,1,1,1, if it is same values then i need to get the value(means in my expample if all my depotid's is 1 i need to get 1 if not i will show by default 1) if not it will show 1 – suresh Nov 12 '18 at 10:05
  • _“Here is my array:”_ - that is only the initialization of an _empty_ array, that you then for some reason call a `val` method on … Please at least show example code that makes sense and matches what you say about it; this one hardly does. – misorude Nov 12 '18 at 10:10
  • @misorude as per your comment i have added my whole fucntion code please look at it – suresh Nov 12 '18 at 10:11

2 Answers2

1

Here is one approach to how you might do this. A Set in javascript can only hold unique values, thus, if its size equates to 1, then the array has all equal values, if its size equates to something greater than one, then all the values are not unique:

Take a look at the snippet below:

const aArr = [1, 2, 3, 4, 5];
const bArr = [1, 1, 1, 1, 1];

const isUniqueArr = arr => {
  const tmp = new Set(arr);
  if(tmp.size > 1) {
    return false;
  }
  return arr[0];
}

console.log(isUniqueArr(aArr)); // expected: false

console.log(isUniqueArr(bArr)); // expected: 1

For your example you can use this function like so:

let res = isUniqueArr(selectedVehiclesDepotIds);

To get the result, where res is either false or the number which appears across the entire array.

This function can be applied to your code like so:

function checkIfAllTheSelectedVehiclesDepotIdsSame(selectedVehiclesDepotIds) 
{ 
  var tmp = new Set(selectedVehiclesDepotIds); 
  if(tmp.size > 1) { 
    return false; 
  } 
  return arr[0]; 
}


var res = checkIfAllTheSelectedVehiclesDepotIdsSame(selectedVehiclesDepotIds) 
if(res == false) { 
  // logged in depot id 
} else { 
  // store the value held in res 
}
// Your createOrderForTicket() goes under here

Then in your createOrderForTicket() function you can do the following:

var res = checkIfAllTheSelectedVehiclesDepotIdsSame(selectedVehiclesDepotIds) 
if(res == false) { 
  // logged in depot id 
} else { 
  // store the value held in res 
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me – suresh Nov 12 '18 at 10:08
  • In the above example, `aArr` is one example of an array, and `bArr` is another example of a given array. So, you can use the function `isUniqueArr` to check any array. – Nick Parsons Nov 12 '18 at 10:12
  • so in my case what are the 2 arrays which i need to check for the condition and please check my question i have edited my question with full code – suresh Nov 12 '18 at 10:14
  • In your question you state " i would like to check the condition like if all the values in array is same then return that value if not return false;" You only need **one** array to do the check. The array which you pass into the `isUniqueArr` is the array which you want to check if it is unique. In my example I used two arrays to demonstrate two different possible outputs of the function. In your case, I'm assuming you just want to check the `selectedVehiclesDepotIds` array to check if it has unique values or not. You can do this by doing `isUniqueArr(selectedVehiclesDepotIds)` – Nick Parsons Nov 12 '18 at 10:17
  • ok so from your example instead of arr i can replace my selectedVehiclesDepotIds? – suresh Nov 12 '18 at 10:18
  • yes, that should work if you use it like so: `isUniqueArr(selectedVehiclesDepotIds)` – Nick Parsons Nov 12 '18 at 10:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183492/discussion-between-nick-parsons-and-suresh). – Nick Parsons Nov 12 '18 at 10:23
1

The following code Should work for you. Fill free to ask any questions you might have.

var arrayTocheck1 = [1,2,3,4];
var arrayTocheck2 = [1,1,1,1];

function checkIfAllTheSame(arr){
    var i = 0;
    var allTheSame = false;
    if(arr && arr.length > 0){
        allTheSame = true;
        var firstElement = arr[0];
        for(i=1; i<arr.length; i++){
            if(arr[i] !== firstElement){
                allTheSame=false;
                i=arr.length;
            }
        }      
    }
    return allTheSame;
}

function getCommon(arr){
    if(checkIfAllTheSame(arr)){
        return arr[0];
    }
    return null;
}

alert(getCommon(arrayTocheck1));
alert(getCommon(arrayTocheck2));
Tornike Shavishvili
  • 1,244
  • 4
  • 16
  • 35