Find partial string in array [duplicate] - javascript

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Closed 3 years ago.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
Let's say you don't know whats inside the fruits array ?
How do you extract the string that contains mango.
The prefix string must be included in the result.
Can this be done without a for loop and parsing ?
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
console.log(n)

You need to filter the array and check each string.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"],
result = fruits.filter(string => string.includes("Mango"));
console.log(result);

How about the following?
fruits.filter(fruit => fruit.includes('Mango'))
// [ 'CarPaint.InString.Mango' ]

Related

String to array js [duplicate]

This question already has an answer here:
Split string into array of equal length strings
(1 answer)
Closed 2 years ago.
I want to convert a String '1234567' to Array
like this['123','456','7']
i have try this:
let str='1234'
let num=3;
let temp='';
let array=str.split('')
let newArr=[];
for(let i = 0;i<array.length;i++){
if((i+1)%num!==0){
temp+=array[i]
}else{
temp+=array[i]
newArr.push(temp)
temp='';
}
}
console.log(newArr)
but it miss the 4
When the loop finishes, temp may be non-empty without having been added to newArr. One way to handle this would be to check for this scenario after the loop and handle accordingly.
// after the for-loop
if (temp) {
newArr.push(temp)
}

Finding match element with array reduce [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
I have two arrays
a. [1,2,3,4,5]
b. [2,3,4,5,6]
I try to find 2,3,4,5 with array.reduce because I think it is more efficient.
Can I do so?
This will get you the same result without using reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
result = a.filter(p=>b.includes(p));
console.log(result);
Or with reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
var result = b.reduce((acc,elem)=>{
if(a.includes(elem)) acc.push(elem);
return acc;
},[]);
console.log(result);
With filter and includes
{
const a = [1,2,3,4,5];
const b = [2,3,4,5,6];
let overlap = a.filter(e => b.includes(e))
console.log(overlap)
}

How can I find match string in array javascript? [duplicate]

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Javascript Searching Array for partial string
(2 answers)
Check for Partial Match in an Array
(5 answers)
Closed 3 years ago.
How can I find a match in an array of strings using Javascript? For example:
var str = "https://exmaple.com/u/xxxx?xx=x";
var filter = ["/u","/p"];
if (!str.includes(filter)){
return 1;
}
In the above code I want to search for var str if match this two values of array filter
This should work:
var str = "https://exmaple.com/u/xxxx?xx=x";
var filters = ["/u","/p"];
for (const filter of filters) {
if (str.includes(filter)) {
console.log('matching filter:', filter);
break; // return 1; if needed
}
}
In your array you need to find the element which includes "/u". filter will return an array and will contain only those element which includes u
var x = ["https://exmaple.com/u/xxxx?xx=x", "https://exmaple.com/p/xxxx?xx=x"];
let matched = x.filter(item => item.includes("/u"));
console.log(matched)
Try this:
var x = ["https://exmaple.com/u/xxxx?xx=x","https://exmaple.com/p/xxxx?xx=x"], i;
for (i = 0; i < x.length; i++) {
if(x[i].includes('/u')) {
document.write(x[i]);
}
}
This loops through all the URLs and picks the one that has /u in it. Then it just prints that out.

put array values in multiple variable Javascript [duplicate]

This question already has answers here:
Javascript equivalent of PHP's list()
(9 answers)
Closed 3 years ago.
I know I can put item by item to variable like:
var array = [15,20];
var a = array[0];
var b = array[1];
But I curious how to put array values in multi variable in one line like list() in php:
list($a,$b) = [15,20]; // $a = 15, $b = 20
Is it possible or not ?!
Use javascript's array de-structuring syntax,
var array = [15,20];
const [x, y] = array;
You can use: Destructuring assignment - Javascript 1.7
matches = ['12', 'watt'];
[value, unit] = matches;
console.log(value,unit)

javascript/jQuery sort array [duplicate]

This question already has answers here:
Sort Array Elements (string with numbers), natural sort
(8 answers)
Closed 8 years ago.
I have an array of values that look like this:
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
I need to sort them from lowest to highest.
The way I would like this array to be is like this:
["S1_FORM", "S2_FORM", "S2_2_FORM", "S3_FORM"]
Basically these numbers should be read like: 1, 2, 2.2, 3
How could I achieve this?
I have tried using .sort() but it returns:
["S1_FORM", "S2_2_FORM", "S2_FORM", "S3_FORM"]
notice "2_2" comes before "2_". It shouldn't.
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
var extractNumber = function(str) {
var m = str.match(/^S(\d+)_(?:(\d+)_)?/);
return parseFloat(m[1] + '.' + m[2])
};
myArr.sort(function(a, b) {
return extractNumber(a) - extractNumber(b);
});
console.log(myArr);
http://jsfiddle.net/LDphK/
So you're extracting a number using trivial regular expression and then sort it using Array.prototype.sort()
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
myArr.sort();
If you need another sorting logic, use jQuery. This is vanilla JavaScript solution

Categories

Resources