Reputation: 577
I have three strings which contain comma separated numbers.
var str1 = "1,5,3";
var str2 = "2,5,1";
var str3 = "3,2,1,5";
I want to check these three strings with each other (To get the common elements between all)
Expected output
var result = 1,5;
If I have only two strings, this is the logic I have already used
var array = str2.split(',');
for(var item in array) {
var contains = (str1.indexOf(array[item]) > -1);
if(contains == 1) {
var result = array[item]+',';
getele2 += result;
geteleresult = getele2.replace(/\,$/, '');
}
}
alert(geteleresult);
but when multiple strings are checked i dont know how to apply sort logic for these. Any idea then please help thanks...
Upvotes: 4
Views: 1762
Reputation: 104810
You can call a function recursively-
after you find the elements common to the first two strings,
compare the commons to the next string, and so on,
for as many arguments as you pass to the function.
This example will return two '5's if all the strings contain two comma separated '5's. (And they needn't be numbers, strings will work the same.)
function inCommon(s1, s2){
var L1= s1.length, L2= s2.length, rx,
A1, next, next1, next2, least, common= [];
A1= (L1<= L2)? s1.split(','):s2.split(',');
for(var i= 0, L= A1.length; i<L; i++){
next= A1[i];
rx= RegExp('(^|,)'+next+'(,|$)', 'g');
next1= (s1.match(rx) || []).length;
next2= (s2.match(rx) || []).length;
least= Math.min(next1, next2);
while(least--) common.push(next);
}
if(arguments.length>2 && common.length){
var a= common.slice.call(arguments, 2);
a.unshift(common.join(','));
return inCommon.apply(window, a);
}
return common;
}
var str1= "1,5,3";
var str2= "2,5,1";
var str3= "3,2,1,5";
inCommon(str1, str2, str3)
/* returned value: (Array)
1,5
*/
Upvotes: 0
Reputation: 193291
What about this simple approach:
function common() {
var arg = [].slice.call(arguments),
com = [], i;
for (i = 1; i < arg.length; i++) {
com = arg[i].match(RegExp((com.length ? com : arg[0].split(',')).join('|'), 'g'));
}
return com;
}
It's very simple, and supports any number of strings. E.g.
var arr = common(str1, str2, str3, str4);
Upvotes: 1
Reputation: 5043
It would be easiest if you could store the strings in an array to start. If you can do so the following should do the trick. This also scales to check more than three if need be with no changes.
str[0] = "1,5,3";
str[1] = "2,5,1";
str[2] = "3,2,1,5";
var matches = '';
for(var i=0;i<str.length-1;i++) {
if(i==0)
var str1 = str[i].split(',');
else
var str1 = matches.split(',');
var str2 = str[i+1].split();
var newMatches = '';
for(var item in str2) {
var contains = (str1.indexOf(array[item]) > -1);
if(contains == 1) {
var result = array[item]+',';
newMatches += result;
}
}
matches = newMatches.replace(/\,$/, '');
}
return matches;
Upvotes: 0
Reputation: 50923
You can try this Function and logic:
function findCommon() {
var nums = {};
var args_length = arguments.length;
for (var i = 0; i < args_length; i++) {
var cur_arg = arguments[i];
var cur_found = {};
var cur_arg_split = cur_arg.split(",");
for (var j = 0; j < cur_arg_split.length; j++) {
var cur_val = cur_arg_split[j];
if (!(cur_val in cur_found)) {
cur_found[cur_val] = 1;
if (!(cur_val in nums)) {
nums[cur_val] = 0;
}
nums[cur_val]++;
}
}
}
var ret = [];
for (var key in nums) {
if (nums[key] === args_length) {
ret.push(key);
}
}
return ret;
}
DEMO: http://jsfiddle.net/LkEyj/1/
It allows you to pass any number of variables (strings that contain comma-separated numbers) to the function.
Upvotes: 1
Reputation: 3755
you can store the value inside an array and then apply .sort() method to sort the values.
var str1 = "1,5,3";
var str2 = "2,5,1";
var str3 = "3,2,1,5";
var getele2="";
var rarray = new Array();
var array = str2.split(',');
for(var item in array) {
var contains = (str1.indexOf(array[item]) > -1);
if(contains == 1){
var result = array[item];
rarray.push(result);
}
}
alert(rarray.sort());
Here is the fiddle : Demo
http://jsfiddle.net/jvaibhav/BsFPW/
Upvotes: 0
Reputation: 5018
You can use indexOf() Method
var str1 = "1,5,3";
var str2 = "2,5,1";
var str3 = "3,2,1,5";
var array1 = str1.split(',');
var array2 = str2.split(',');
var array3 = str3.split(',');
var output = [];
for(var i=0;i<array1.length;i++)
{
if(array2.indexOf(array1[i]) >= 0 && array3.indexOf(array1[i]) >= 0)
output.push(array1[i]);
}
alert(output);
Upvotes: 0