How to find the string that matches in array? - javascript

I would like to get the full string element from an array that matches multiple substrings in no particular order. If more than one match, an exception should be thrown.
var thisArray = [ '/something_else/', '/and_something_else/', '/and_something_else_here/'];
var thisMatch = [ 'some', 'and', 'here'];
var matchingElement = new RegExp(thisArray , thisMatch); // Not sure about this
What I want is for matchineElement to contain the string "and_something_else_here" after the regular expression has executed.

You could do something like this:
var thisArray = [ '/something_else/', '/and_something_else/', '/and_something_else_here/'];
var thisMatch = [ 'some', 'and', 'here'];
function matchElements(arr, arrMatch) {
var tmpArr;
arrMatch.forEach(function(el, i, a) {
tmpArr = arr.filter(function(str) {
return str.indexOf(el) !== -1;
});
});
if (tmpArr.length > 1) {
throw ("Hey, too many matches");
} else {
return tmpArr[0];
}
}
console.log(matchElements(thisArray, thisMatch));
JSFiddle: http://jsfiddle.net/Le53y7ee/
Explanation:
The function goes through every element in the array containing substrings and filters main array keeping only strings that match. After the loop is done the array should contain only string(s) that matched all required substrings.

A regexp that matches a string containing all of a set of substrings looks like:
^(?=.*?string1)(?=.*?string2)(?=.*?string3).*$
So you just need to convert your thisMatch array into such a regular expression.
var regexp = new RegExp('^' + thisMatch.map(function(x) {
return '(?=.*?' + x + ')';
}).join('') + '.*$');
Then you can find all the matches with:
var matchingElements = thisArray.filter(function(x) {
return regexp.test(x);
});
if (matchingElements.length != 1) {
throw new Error('Single match not found');
} else {
matchingElement = matchingElements[0];
}
DEMO

Try
var matchingElement = thisArray.filter(function(val, key) {
var value = val.split(/[^a-z]/).filter(String).join(" ");
// if `.test()` returns `true` three times at `value`
return new RegExp(this[0]).test(value)
&& new RegExp(this[1]).test(value)
&& new RegExp(this[2]).test(value)
? val
: null
}.bind(thisMatch))[0].replace(/\//g,"");)
var thisArray = [ '/something_else/', '/and_something_else/', '/and_something_else_here/'];
var thisMatch = [ 'some', 'and', 'here'];
var matchingElement = thisArray.filter(function(val, key) {
var value = val.split(/[^a-z]/).filter(String).join(" ");
// if `.test()` returns `true` three times at `value`
return new RegExp(this[0]).test(value)
&& new RegExp(this[1]).test(value)
&& new RegExp(this[2]).test(value)
? val
: null
}.bind(thisMatch))[0].replace(/\//g,"");
document.write(matchingElement)

Take those slashes off of both sides of your Strings in those Arrays. Then use the arrayMatch function below, and loop over your indexes to get the results of thisArray, like:
function arrayMatch(yourArray, wordMatchArray){
var ix = [], c = wordMatchArray.length;
for(var i=0,l=yourArray.length; i<l; i++){
var m = 0;
for(var n=0; n<c; n++){
if(yourArray[i].match(new RegExp(wordMatchArray[n])))m++;
if(c === m)ix.push(i);
}
}
if(!ix[0]){
return false;
}
return ix;
}
var indexes = arrayMatch(thisArray, thisMatch);
for(var i=0,l=indexes.length; i<l; i++){
var getMatch = thisArray[indexes[i]];
}

Related

Function is meant to be non-destructive but it is modifying my variable

I have the following code which sets special to run a function convertSpecial which will replace the apostrophe in array1 with a provided character. In this case, a space. Since the replacing character is a space, it will split that element into two then flatten the array. It will then check to see if any element in special matches any element in array2. This will return false. It will then replace the apostrophe with no character at all and recheck against array2.
The idea behind convertSpecial and the variable special is that it should be non-destructive to array1, but this isn't happening as you can see:
var array1 = ["o'hara"];
var array2 = ["ohara"];
var special = '';
function convertSpecial(a,b,c) {
var aCopy = a;
for (let i = 0; i < aCopy.length; i++) {
if (aCopy[i].includes(b)) {
if (c == '') {
aCopy[i] = aCopy[i].replace(b,c);
} else {
aCopy[i] = aCopy[i].replace(b,c).split(' ');
aCopy = aCopy.flat();
}
}
}
return aCopy;
}
console.log('array1 1 = '+array1); // returns array1 1 = o'hara as expected
special = convertSpecial(array1,"'"," ");
console.log('array1 2 = '+array1); // returns array1 2 = o,hara THIS SHOULD NOT HAVE BEEN MODIFIED
console.log('special 1 = '+special); //returns special 2 = o,hara as expected
if (array2.some(v => special.includes(v)) == true) {
console.log('array1 3 = '+array1); // ignored as expected
array1 = specialDECln;
} else {
console.log('array1 4 = '+array1); //returns array1 4 = o,hara THIS SHOULD NOT HAVE BEEN MODIFIED
special = convertSpecial(array1,"'","");
console.log('array1 5 = '+array1); //returns array1 5 = o,hara THIS SHOULD NOT HAVE BEEN MODIFIED
console.log('special 2= '+special); //returns special 2 = o,hara should be ohara
if (array2.some(v => special.includes(v)) == true) {
array1 = special;
}
}
console.log(array2 == special); //returns false, should be true because expected ohara = ohara
Everything works as it should, EXCEPT array1 is being modified when it shouldn't be at all. Since it gets modified, special gets set to an incorrect value.
What am I doing wrong and how can I fix it?
You need to clone that array in order to avoid mutation. There are a lot of ways for doing that, an alternative is using the Spread-syntax as follow:
let result = [...a]; // This creates a new array with the index-values from array a.
var array1 = ["o'hara"];
var special = ''
console.log("array1 = " + array1); // returns array1 = o'hara as expected
function convertSpecial(a, b, c) { // a = array, b = character to replace, c = character to replace with
let result = [...a];
for (let i = 0; i < result.length; i++) {
if (result[i].includes(b)) {
if (c == '') {
result[i] = result[i].replace(b, c);
} else {
result[i] = result[i].replace(b, c).split(' ');
result = result.flat();
}
}
}
return result;
}
special = convertSpecial(array1, "'", " ");
console.log("array1 = " + array1); // returns array1 = o, hara but it should be o'hara
console.log("special = " + special); // returns special = o, hara as expected
You need to duplicate the array first:
var array1 = ["o'hara"];
var special = ''
console.log("array1 = " + array1); // returns array1 = o'hara as expected
function convertSpecial(a, b, c) { // a = array, b = character to replace, c = character to replace with
var aCopy = a.slice();
for (let i = 0; i < aCopy.length; i++) {
if (aCopy[i].includes(b)) {
if (c == '') {
aCopy[i] = aCopy[i].replace(b, c);
} else {
aCopy[i] = aCopy[i].replace(b, c).split(' ');
aCopy = aCopy.flat();
}
}
}
return aCopy;
}
special = convertSpecial(array1, "'", " ");
console.log("array1 = " + array1); // returns array1 = o, hara but it should be o'hara
console.log("special = " + special); // returns special = o, hara as expected

How to compare strings in an array: Javascript

I have an array which looks like
var arr = ["a|c", "a|e", "x|z"];
for(var x in arr){
var appsplit = x.split("|");
}
If the first value(ex: a) in the elements matches then it should combine the values
Ex: output
ace
xz
Please advice how this approach can be done.
You are testing everyone's reading comprehension with that riddle.
var pairs = {};
var arr = ["a|c", "a|e", "x|z"];
for(var x in arr)
{
var appsplit = arr[x].split("|");
if(pairs[appsplit[0]] !== "undefined")
{
pairs[appsplit[0]] = pairs[appsplit[0]] + appsplit[1];
}
else
{
pairs[appsplit[0]] = appsplit[1];
}
}
var matches = [];
for(var x in pairs)
{
matches.push(x + pairs[x]);
}
console.log(matches);
We need to map out the arr elements in this object called pairs. The first value in your split would be the key and the second value is appended (or assigned if it's the first match to the key)
You made an error of splitting x, but you are only splitting the index of the element, not the actual value of the element. arr[x] is the actual value, where x specifies the index in the array.
After we've gone through your arr, we can now merge the key with the values. Your output is contained in matches where the key in each pair is prepended to the value of the key's pair.
Some simple code that would to the trick here.
var arr = ["a|c", "a|e", "x|z", "c|b", "z|e", "c|a"];
var resultObj = {};
arr.forEach(function(element, index){
var array = element.split('|');
if(array.length!==2){
console.log("skipping, invalid input data", element);
} else {
var firstLetter = array[0];
var secondLetter = array[1];
if(resultObj[firstLetter]){
resultObj[firstLetter].push(secondLetter);
} else {
resultObj[firstLetter]=[secondLetter];
}
}
});
Object.keys(resultObj).forEach(function(key){
console.log(key + "," + resultObj[key]);
});
You can use .reduce(), Set to not accumulate duplicate values, .some() to check if previous array contains value in current array, .map(), Array.from() and .join() to convert array to string
var arr = ["a|c", "a|e", "x|z"];
var res = arr.reduce(function(a, b) {
var curr = b.split("|");
var set = new Set;
for (let prop of curr) set.add(prop);
if (!a.length) {
a.push(set)
} else {
for (prop of a) {
if (curr.some(function(el) {
return prop.has(el)
})) {
for (el of curr) {
prop.add(el)
}
} else {
for (let prop of curr) set.add(prop);
a.push(set)
}
}
}
return a
}, []).map(function(m) {
return Array.from([...m], function(el) {
return el
}).join("")
});
console.log(res);
I feel like this can be done more elegantly, but I didn't have time to streamline it. :) The below code will do what you want, though:
var aStartArray = **ARRAY_VALUE_HERE**;
var aSplitResultStrings = [];
// step through each element in the array
for (var i = 0, iSALength = aStartArray.length; i < iSALength; i++) {
// split the values for the current array element
var aSplitVal = aStartArray[i].split("|");
var bStringDoesNotExist = true;
// loop through the "result strings" array
for (var j = 0, iSRSLength = aSplitResultStrings.length; j < iSRSLength; j++) {
// if the first letter from the array element = the first letter of the current "result string" . . .
if (aSplitResultStrings[j].charAt(0) === aSplitVal[0]) {
// append the second letter of the array value to the current result string
aSplitResultStrings[j] = aSplitResultStrings[j] + aSplitVal[1];
// indicate that a match has been found and exit the "result string" loop
bStringDoesNotExist = false;
break;
}
}
// if there are no result strings that start with the first letter of the array value . . .
if (bStringDoesNotExist) {
// concatenate the two values in the current array value and add them as a new "result string"
aSplitResultStrings.push(aSplitVal[0] + aSplitVal[1]);
}
}
Using these arrays, the results are:
aStartArray = ["a|c", "a|e", "x|z"] //results in:
aSplitResultStrings = ["ace", "xz"]
aStartArray = ["a|b", "a|c", "a|d", "a|e", "x|y", "x|z"] //results in:
aSplitResultStrings = ["abcde", "xyz"]
aStartArray = ["a|b", "d|e", "d|f", "x|y", "g|h", "g|i", "m|n", "g|j", "a|c", "x|z"] //results in:
aSplitResultStrings = ["abc", "def", "xyz", "ghij", "mn"]
As I said, this could be more elegant (for example, you could probably use Map to make iterating through the "result strings" easier), but this makes the steps pretty clear and should get you going down the right path towards a final solution.

how to cut a string in javascript

I'm receiving a String like this:
"45,21,555,64,94,796,488,\n " the \n means new line
is there a way to cut the string based on "," and getting only the "number".
I could do it C but how can I search for a character in JavaScript.
thanks for any hint
var parts = "45,21,555,64,94,796,488,\n ".split(',').filter(function(val) {
var num = parseInt(val, 10);
return !isNaN(num) && toString.call(num) === '[object Number]';
});
// parts: ["45", "21", "555", "64", "94", "796", "488"]
This is taking your String and splitting it into an Array based on a delimiter (',') and then running it through a filter function to remove anything that does not evaluate to a valid Number.
See String.prototype.split and Array.prototype.filter.
If you actually want to then convert those values to Numbers, you could chain a map call:
var parts = "45,21,555,64,94,796,488,\n ".split(',')
.filter(function(val) {
var num = parseInt(val, 10);
return !isNaN(num) && toString.call(num) === '[object Number]';
})
.map(function(val) {
return parseInt(val, 10);
});
// parts: [45, 21, 555, 64, 94, 796, 488]
Yes, like this:
var myString = "45,21,555,64,94,796,488,\n ";
var splitStrings = string.split(",");
console.log(splitStrings); //Should log an array to the console, containing only your strings e.g. [45,21,555,64,94,796,488,\n]
This returns an array of strings, split by the character you passed in. You can read more on this method here: http://www.w3schools.com/jsref/jsref_split.asp
After that, you can parse your array to remove anything you don't want like the new line character, or use a filter method to do it inline as detailed in another answer
You can use string.split(splitChar) to split. Then you can use .map or .filter to convert items to number.
var str = "45,21,555,64,94,796,488,\n ";
var arr = str.split(",");
document.write("Array: <pre>"+JSON.stringify(arr)+"</pre>");
var nums = arr.map(function(item){
return parseInt(item);
});
document.write("Numbers: <pre>"+JSON.stringify(nums)+"</pre>");
If you are wanting just the numbers as an array you could do this.
var str = "45,21,555,64,94,796,488,\n ";
var arr = str.split(","); //Make the string into an array
var len = arr.length;
for(var i=0;i<len;i++)
{
try
{
arr[i] = parseInt(arr[i]); // convert the numbers to ints
}
catch(e)
{
arr[i] = null;
}
}
As the fact that strings are just arrays, you can do it this way (a bit more comprehensive, in my opinion):
function noCommas(a)
{
var b = '';
for (var i = 0; i < a.length; i++)
{
if (a[i] != ',') //&& a[i]!= String.fromCharCode(10)) If you want no new lines too
{
b += a[i];
} else
{
//break; -> In case you only want the first value
}
}
return parseInt(b); //To return the value as an integer
}
noCommas('1234,6789'); // returns 12346789
I missunderstood the question, so here's my fixed code:
function noCommas(a)
{
var b = [];
var tempNum = '';
for (var i = 0; i < a.length; i++)
{
if (a[i] != ',' && i != a.length - 1)
{
tempNum += a[i];
} else
{
b.push(parseInt(tempNum))
tempNum = '';
}
}
return b;
}
now if you do noCommas('123,345,324234')[2] it would return 324234.

Loop thru a string with numbers and symbols & push to array

I'm trying to loop thru a string with numbers that has a symbol inside, I want the numbers before the symbol to be pushed to an array then the symbol to another array and then get the rest of the numbers after the symbol pushed to a 3rd array.
var myString = "1234*5678";
var number1 = [];
for (i = 0; i < myString.length; i++) {
if (isNaN(myString[i]) === false) {
var firstSet = number1.push(myString[i]);
}
};
var mySymbol = [];
for (j = number1[0]; j < myString.length; j++) {
if (isNaN(myString[j]) === true) {
var mathematics = mySymbol.push(myString[j])
document.write(mySymbol[0])
}
};
when I document.write the "mySymbol" variable it gives the desired result, but when I call the "number1" variable it gives me the numbers from before and after the symbol I only want the numbers before the symbol to be pushed to the array, also how do I write the 3rd loop to get the numbers after the symbol pushed to a new array?
try
var arr=[[],[],[]]
index = 0
"1234*5678".split('').forEach(function(e){
if(parseInt(e)){
arr[index].push(e);
}else{
index ++;
arr[index++].push(e)
}
});
document.write('First Array ' +arr[0] + '<br>');
document.write('Secont Array ' +arr[1] + '<br>');
document.write('Third Array ' +arr[2]);
You could try using:
var myString = '1234*5678';
var resultArr = myString.match(/([a-zA-Z0-9]+)([^a-zA-Z0-9])([a-zA-Z0-9]+)/);
To get the string before symbol:
var myFirstSet = resultArr[1];
To get symbol:
var mySymbol = resultArr[2];
To get the string after symbol:
var mySecondSet = resultArr[3];
To convert each of these three groups into their own arrays:
var result = [];
resultArr
.slice(1)
.forEach(
function(s){
result.push(s
.split('')
.map(
function(n){
return parseInt(n) || n;
}
)
)
}
);
The following code accomplishes the goal:
var str = '1234*5678';
var arr = str.match(/([a-zA-Z0-9]+)|\*/g);
console.log(arr[0], arr[1], arr[2]);
http://jsfiddle.net/hp2ohvzb/

how to get values from an array in javascript?

let the array be
var array=
[
"me=Salman","Profession=student","class=highschool"
]
How do I extract the value of 'me' here?
Try this:
var result = '';
for(var values in array){
if(values.indexOf('me=') !== -1 ){
result = values.split('=')[1];
break;
}
}
You will need to search the array for your desired portion of the string, then remove what you searched for from the indicated string.
var array = [ "me=Salman" , "Profession=student" , "class=highschool" ];
var findMatch = "me=";
var foundString = "Did not find a match for '"+findMatch+"'.";
var i = 0;
for (i = 0; i<array.length; i++) //search the array
{
if(array[i].indexOf(findMatch) != -1) // if a match is found
{
foundString = array[i]; //set current index to foundString
foundString = foundString.substring(findMatch.length, array[i].length); //remove 'me=' from found string
}
}
Try this:
var a = [ "me=Salman" , "Profession=student" , "class=highschool" ];
var result = a.filter(function(e){return /me=/.test(e);})[0]; // Find element in array
result = result.length ? result.split('=').pop() : null; // Get value
Or function:
var array = [ "me=Salman" , "Profession=student" , "class=highschool" ];
function getVal(arr, key){
var reg = new RegExp(key + '=');
var result = arr.filter(function(e){ return reg.test(e)})[0];
return result.length ? result.split('=').pop() : null;
}
console.log( getMe(array, 'me') );

Categories