How can I put middle dot character inside .join()? - javascript

How can I put middle dot character inside .join() for split arrays?
I should put this
<span>·</span>
inside
{product.user.map(ul => ul.names).join(" ")}

If I get the questions correctly, you just need to use dot instead of empty quotation marks.
A Sample modified from w3schools.
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = document.getElementById("demo");
x.innerHTML = fruits.join('.');
}
<!DOCTYPE html>
<html>
<body>
<p>Click the button to join the array elements into a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>

Yes you can, this should work:
<h1>{product.user.map(ul => ul.names).join(' <span>·</span> ')} </h1>

Related

How to split string from particular word in javascript?

[
{
docPath: "c:\Project\Indofebweb\Attachment\images\indofab.png",
}
]
I want to split the string from \Attachment and get the rest of the string e.g
\Attachment\images\indofab.png . How can I do this?
For this particular string you can use :
var arr=test.split("Indofebweb");
var restString=arr[1]; //-----contains string \Attachment\images\indofab.png
You can make your own logic using split() function.
UPDATED CODE----TRY THIS----BELOW HTML
<!DOCTYPE html>
<html>
<body>
<p> a string display :</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "c:\\Project\\Indofebweb\\Attachment\\images\\indofab.png";;
var arr=str.split("Indofebweb");
document.getElementById("demo").innerHTML = arr[1];
}
</script>
</body>
</html>
use str.match()
path.match(/\\Attachment.*/);
this will return everything after and including "\Attachment". Keep in mind that backslashes need to be escaped.

How to replace "#[" anywhere in the string

My question is about regular expressions. I want to replace "#[" anywhere in the string. Tried with [#[] expression, but it's not working, because it replaces # anywhere in the string.
you can use the regex
/#\[/g
function replace(str){
return str.replace(/#\[/g, '');
}
console.log(replace('#[]'));
console.log(replace('abcd#[absd]ahah#[ahah'));
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">Visit Microsoft! #[</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/#\[/g, "W3Schools");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Array not showing length

Been trying for about an hour now, can't get my array to show its length upon a button press. It did show its length at one attempt, but it was a totally different amount to the amount of elements in the array. I read somewhere that it happens that way sometimes, and has something to do with undefined elements. But now it isn't showing any alerts at all.
var fruits("Banana", "Apple", "Orange")
function myFunction() {
alert(fruits.length)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<body>
<script>
src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" >
</script>
<script src="Arrays.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
</head>
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
</body>
</html>
If you want to declare an array using () you need to use new Array():
var fruits = new Array("Banana", "Apple", "Orange")
function myFunction() {
alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
var fruits = new Array("Banana","Apple", "Orange" );
var fruitsArr = ["Banana","Apple", "Orange" ];
function myFunction(arr) {
console.log(arr.length)
}
myFunction(fruits)
myFunction(fruitsArr)
You can create an array using array literal i.e [] or you can use new Array() to create an array.
Your array declaration is JavaScript code is incorrect. It should be like.
var fruits = ["Banana","Apple","Orange"]
var fruits = ["Banana", "Apple", "Orange"];
function myFunction() {
alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()">Find Length of Array</button>
The array literal syntax in JavaScript uses square brackets:
var fruits = [ "Banana","Apple", "Orange" ]
var fruits = ["Banana","Apple", "Orange"]
Your code has something wrong.
You need to use () with new Array() or you can use [] if you do not want to declare it explicitly.
e.g.
var fruits = new Array("Banana", "Apple", "Orange")
OR
var fruits = ["Banana", "Apple", "Orange"]
var fruits = ["Banana", "Apple", "Orange"]
function myFunction() {
alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>
OR
var fruits = new Array("Banana", "Apple", "Orange")
function myFunction() {
alert(fruits.length)
}
<button id="lengthbutton" onclick="myFunction()"> Click me to find out length </button>

split hindi word maintaining conjuncts using javascript

i am trying out bellow code
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "कसौटी";
var res = str.split("");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
my result should be क, सौ, टी
however it turns out to be क,स,ौ,ट,ी
what exactly can be done to get the above result
is there a way to split hindi character while maintaing the conjuncts
any way in which we split utf -8 encoded characters then merge them back to get above result
please provide details in code , on how exactly it can be done

Javascript replace words from a string

I have this text in javascript:
'This is a %{component} with a %{value}'
I should replace all words that look like %{\w+} with <span>word</span>
The final result should look like:
'This is a <span>component</span> with a <span>value</span>'
As already written use String.replace and a RegEx:
var originalString = 'This is a %{component} with a %{value}';
var replacedString = originalString.replace(/%\{(\w+)\}/g, '<span>$1</span>');
// "This is a <span>component</span> with a <span>value</span>"
Is this what you are looking for?
<!DOCTYPE html>
<html>
<body>
<p id="demo">This is a %{component} with a %{value}</p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML;
var n=str.replace("%{","< span >").replace("}","< /span >");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
You can do that with regex in javascript / jquery.
Have a look here: http://de.selfhtml.org/javascript/objekte/regexp.htm

Categories