check whether all the values in array is same or not in javascript











up vote
0
down vote

favorite












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();
}









share|improve this question
























  • Possible duplicate of Check if all values of array are equal
    – Zenoo
    Nov 12 at 9:56










  • A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
    – SPlatten
    Nov 12 at 9:57










  • i didn't understand can you please explain clearly
    – suresh
    Nov 12 at 9: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 at 9: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 at 10:03















up vote
0
down vote

favorite












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();
}









share|improve this question
























  • Possible duplicate of Check if all values of array are equal
    – Zenoo
    Nov 12 at 9:56










  • A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
    – SPlatten
    Nov 12 at 9:57










  • i didn't understand can you please explain clearly
    – suresh
    Nov 12 at 9: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 at 9: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 at 10:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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();
}









share|improve this question















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();
}






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 10:11

























asked Nov 12 at 9:55









suresh

29410




29410












  • Possible duplicate of Check if all values of array are equal
    – Zenoo
    Nov 12 at 9:56










  • A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
    – SPlatten
    Nov 12 at 9:57










  • i didn't understand can you please explain clearly
    – suresh
    Nov 12 at 9: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 at 9: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 at 10:03


















  • Possible duplicate of Check if all values of array are equal
    – Zenoo
    Nov 12 at 9:56










  • A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
    – SPlatten
    Nov 12 at 9:57










  • i didn't understand can you please explain clearly
    – suresh
    Nov 12 at 9: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 at 9: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 at 10:03
















Possible duplicate of Check if all values of array are equal
– Zenoo
Nov 12 at 9:56




Possible duplicate of Check if all values of array are equal
– Zenoo
Nov 12 at 9:56












A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
– SPlatten
Nov 12 at 9:57




A simple loop will do, start off by comparing length, if both lengths are the same then compare each element.
– SPlatten
Nov 12 at 9:57












i didn't understand can you please explain clearly
– suresh
Nov 12 at 9:59




i didn't understand can you please explain clearly
– suresh
Nov 12 at 9: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 at 9: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 at 9: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 at 10:03




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 at 10:03












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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
}





share|improve this answer























  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
    – suresh
    Nov 12 at 10:08












  • can you please modfy your code as per my code
    – suresh
    Nov 12 at 10:12










  • 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 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 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 at 10:17




















up vote
1
down vote













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));





share|improve this answer























  • let me check your code
    – suresh
    Nov 12 at 10:28










  • @suresh i have edited my answer for syntax errors. please check the latest one.
    – Tornike Shavishvili
    Nov 12 at 10:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259656%2fcheck-whether-all-the-values-in-array-is-same-or-not-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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
}





share|improve this answer























  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
    – suresh
    Nov 12 at 10:08












  • can you please modfy your code as per my code
    – suresh
    Nov 12 at 10:12










  • 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 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 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 at 10:17

















up vote
1
down vote



accepted










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
}





share|improve this answer























  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
    – suresh
    Nov 12 at 10:08












  • can you please modfy your code as per my code
    – suresh
    Nov 12 at 10:12










  • 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 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 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 at 10:17















up vote
1
down vote



accepted







up vote
1
down vote



accepted






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
}





share|improve this answer














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
}





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





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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 11:14

























answered Nov 12 at 10:07









Nick Parsons

4,4012721




4,4012721












  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
    – suresh
    Nov 12 at 10:08












  • can you please modfy your code as per my code
    – suresh
    Nov 12 at 10:12










  • 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 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 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 at 10:17




















  • how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
    – suresh
    Nov 12 at 10:08












  • can you please modfy your code as per my code
    – suresh
    Nov 12 at 10:12










  • 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 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 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 at 10:17


















how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
– suresh
Nov 12 at 10:08






how can i approach in my case..sorry i am new to js so littile bit difficult to understand for me
– suresh
Nov 12 at 10:08














can you please modfy your code as per my code
– suresh
Nov 12 at 10:12




can you please modfy your code as per my code
– suresh
Nov 12 at 10:12












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 at 10:12






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 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 at 10:14






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 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 at 10:17






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 at 10:17














up vote
1
down vote













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));





share|improve this answer























  • let me check your code
    – suresh
    Nov 12 at 10:28










  • @suresh i have edited my answer for syntax errors. please check the latest one.
    – Tornike Shavishvili
    Nov 12 at 10:29















up vote
1
down vote













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));





share|improve this answer























  • let me check your code
    – suresh
    Nov 12 at 10:28










  • @suresh i have edited my answer for syntax errors. please check the latest one.
    – Tornike Shavishvili
    Nov 12 at 10:29













up vote
1
down vote










up vote
1
down vote









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));





share|improve this answer














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));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 10:29

























answered Nov 12 at 10:19









Tornike Shavishvili

61011022




61011022












  • let me check your code
    – suresh
    Nov 12 at 10:28










  • @suresh i have edited my answer for syntax errors. please check the latest one.
    – Tornike Shavishvili
    Nov 12 at 10:29


















  • let me check your code
    – suresh
    Nov 12 at 10:28










  • @suresh i have edited my answer for syntax errors. please check the latest one.
    – Tornike Shavishvili
    Nov 12 at 10:29
















let me check your code
– suresh
Nov 12 at 10:28




let me check your code
– suresh
Nov 12 at 10:28












@suresh i have edited my answer for syntax errors. please check the latest one.
– Tornike Shavishvili
Nov 12 at 10:29




@suresh i have edited my answer for syntax errors. please check the latest one.
– Tornike Shavishvili
Nov 12 at 10:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259656%2fcheck-whether-all-the-values-in-array-is-same-or-not-in-javascript%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python