Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have an array-like
[
'email1#provider.com;email2#provider.com',
'email3#provider.com',
'email4#provider.com;email5#provider.com;email6#provider.com'
]
Which is the best way to map and split every semicolon on a string in order to form an array with each email separated?
If you are in a modern browser or environment, flatMap() is really nice for splitting each item and flattening into an array.
const l= [
'email1#provider.com;email2#provider.com',
'email3#provider.com',
'email4#provider.com;email5#provider.com;email6#provider.com'
]
const res = l.flatMap(s => s.split(';'))
console.log(res)
You can use reduce:
const original = [
'email1#provider.com;email2#provider.com',
'email3#provider.com',
'email4#provider.com;email5#provider.com;email6#provider.com'
];
const result = original.reduce((res, str) => {
return res.concat(str.split(';'));
}, []);
console.log(result);
In this case you can try my way:
const arr = [
"email1#provider.com;email2#provider.com",
"email3#provider.com",
"email4#provider.com;email5#provider.com;email6#provider.com",
];
const newArr = arr.join(";").split(";");
console.log(newArr);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need help finding the string that matches specific patterns in an array of strings
For example
var array = ['hello there heretic', "purge the alien", "FOR THE EMPEROR!!" ]
How would I grab "FOR THE EMPEROR!!" if I want to find it by the following 2 separate scenarios:
Grab string in array which starts with "FOR"
Grab string in array that contains "EMPEROR"
They need to be ES5 or below though.
You can use the RegEx for checking the given string matching the requirements. Like this,
var regEx = /(^FOR)|(.*EMPEROR.*)/i;
var array = ['hello there heretic', "purge the alien", "FOR THE EMPEROR!!" ]
array.filter(function(str) { return regEx.test(str) }) // ["FOR THE EMPEROR!!"]
For case-sensitive remove i in regex like: /(^FOR)|(.*EMPEROR.*)/
var regEx = /(^FOR)|(.*EMPEROR.*)/i;
var array = ['hello there heretic', "purge the alien", "FOR THE EMPEROR!!", "For the champion", "And the EMPEROR" ]
const result = array.filter(function(str) { return regEx.test(str) })
console.log({result})
If you need to support lower version of IE, use indexOf instead of
includes.
let array = ['hello there heretic', "purge the alien", "FOR THE EMPEROR!!"];
console.log(array.filter( function(el) {
return el.indexOf("EMPEROR") > -1 && el.split(" ")[0] == "FOR"
}))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
On a React Page inside render I have the code below, which produces the error:
topic.language.map is not a function
{console.log(JSON.stringify(topic.language))} //this returns "English,French,Other"
{topic.language.map(
(ln, i) => {
return (
<div
key={i}
className="language"
>
{ln}
</div>
);
}
)}
What am I doing wrong and how can I map the different languages? Do I need .split instead of .map? What would that look like?
If your language value is a string, you will need to split the string into an array before calling map.
const topic = { language: 'English,French,Other' };
topic.language.split(/,/g).map((ln, i) => {
console.log(ln);
});
//assuming your string is like "part1,part2,part3......partn"
//you can just use
//this will return an array of string spitted by your delimiter
//here is ',' comma
//put that is split method and you will be good to go
let stringSplitArray = "part1,part2,part3,part4".split(",")
//now you can do
stringSplitArray.map(element=>(
<div>{element}</div>
))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I wanna check if a number is not a property in an object and if is not add it to the object as a property.
var obj{}
var arr=[1,2,3,4]
I´ve tried if(! number in object ) and if(! object.hasOwnProperty(number)) but didn´t work, and in both cases, I also tried to make the number become a string doing number.toString(), and nothing happened.
ar.forEach(number=>{
if(!(number in obj)){ //or number.toString()
})
ar.forEach(number=>{
if(! obj.hasOwnProperty(number)){ //or number.toString()
})
Your code actually works once you fix the typos:
var obj = {3: 'hey'}
var arr = [1, 2, 3, 4]
arr.forEach(number => {
if (!(number in obj)) {
console.log(number + ' is not in obj')
}
}
)
Object.values(your_object) gives you an array with all the values in your object.
Then iterate trough that array and check if it contains your number.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So, hello. I edited the entire thing.
app.get('/', async (req, res) => {
let results = await db.collection("malwarepad-website").find("6047667ff156cb8135bdaa88").toArray()
//var resultsConverted = results.toString();
//let resultsFinal = resultsConverted.split('"');
console.log(results)
res.render('index.ejs', { startText: results });
})
In the above code I want to only keep the second part of it specified better in this image: https://i.stack.imgur.com/Wi031.png
I want to create a variable containing the following:
Hello, and welcome to my website. I don't know how you found me but yo...
I already have a constant containing the search results, but it is this:
[
{
_id: 6047667ff156cb8135bdaa88,
mainPage: "Hello, and welcome to my website. I don't know how you found me but you're welcome :)."
}
]
Thanks for the understanding :)
a = a.split("\"")[1]
If you mean extracting what's inside double quotations, you have two methods:
1 - Use Regular Expressions:
You can use regular expression /.*"(.*)".*/ which tries to capture everything inside parentheses. You can use exec method. like :
const importantPart = /.*"(.*)".*/.exec(a)[1] (a is your variable)
2 - Using indexOf string methods
In JavaScript strings have two useful methods: indexOf and lastIndexOf. In addition to a substring.
You can use these to extract the important part:
a.substring(a.indexOf('"') + 1, a.lastIndexOf('"'))
There are several solutions. One could be:
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let a_splitted = a.split('"');
console.log(a_splitted[1]);
You can use regular expressions to extract the part that you need.
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let result = a.match(/\"(.*)\"/);
console.log(result[1]);
There are a lot of what-ifs though.
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let regex = /(?<=\")(.*?)(?=\")/;
let result = regex.exec(a)[0];
console.log(result);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
https://jsfiddle.net/a/2L4t9saq/217/ is my fiddle
most of the code you can ignore, here is the function:
var modGrid = function(code){
var arr = code
console.log(arr)
for(var n=1;n<gridx+1;n++){
for(var i = 1; i<gridy+1; i++){
var garbledMess = "[x="+i+"][y="+n+"]"
var idea = arr[0]
arr.shift()
$(garbledMess).css("background-color",idea)
}
}
}
the syntax error is as follows:
Uncaught TypeError: arr.shift is not a function
at modGrid ((index):44)
at window.onload ((index):81)
since the modGrid function takes in an array (in the case of my code an array of 4 elements) the .shift() function should be removing the first option in the array, it worked before i added some more code, but now it is apparently not a function
many thanks
since the modGrid function takes in an array
It is designed to take an array, but that isn't what you are passing it.
You are passing it a string, another string, a number and another number.
modGrid('rgba(255,0,0,1)','rgba(0,255,0,1)',2,1);