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 8 years ago.
Improve this question
I've tried to see if there were any scripts to convert a list of words into an array and I can't seem to find one.
Anyone know where I can find one?
Input:
Dog
Cat
Hamster
Gets converted to
["Dog", "Cat", "Hamster"]
Noo.. this isn't what I mean. I have a txt file with a bunch of words on each line and I was wondering if there was something out there already created that can convert each word into an array.
Just use split on the string.
For example:
var textarea = document.getElementById('list');
var arr = [];
textarea.addEventListener('input', function () {
arr = this.value.split('\n');
console.log(arr);
}, false);
Demo
If the string is actually "Dog\nCat\nHamster" then just do
"Dog\nCat\nHamster".split('\n');//returns ["Dog", "Cat", "Hamster"]
For a TextArea try this
myTextArea.value.split('\n'); // it will return desired output.
Where myTextArea is the TextArea you can get using getElement.
I think the best solution for a textarea input is using the match() function of javascript like this:
var words = []
$('textarea').on('blur', function(){
words = $(this).val().match(/\w+/g)
alert(words)
})
here a fiddle of it working.
This doesn't need for the words to be in different lines.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I just copy and pasted this code from the Internet and it is working, but how do I change the font and text position? I'm working with HTML and I don't know any Javascript.
the script in html:
<script>
const ResultsList = document.getElementById('results')
new URLSearchParams(window.location.search).forEach((value, name) => {
ResultsList.append(`${name}: ${value}`)
ResultsList.append(document.createElement('br'))
})
</script>
Everywhere I do .style.fontWeight="bold"; seems to break the code.
For example, if I do:
<script>
const ResultsList = document.getElementById('results').style.fontWeight="bold";
new URLSearchParams(window.location.search).forEach((value, name) => {
ResultsList.append(`${name}: ${value}`)
ResultsList.append(document.createElement('br'))
})
</script>
The result will simply not even show up on the webpage.
The value of an assignment expression is the value that was assigned. So
const ResultsList = document.getElementById('results').style.fontWeight="bold";
sets ResultsList to "bold". Then you'll get an error when you try to use ResultsList.append(...), because strings don't have an append() method, and you're not appending to the element.
You need to split this into two statements.
const ResultsList = document.getElementById('results');
ResultsList.style.fontWeight = "bold";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.
function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}
Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"
With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1
Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If I had a textarea and a user pasted a paragraph into the textarea, can statistics be run on the input using JavaScript on the client side? I was thinking of using an associative array or hash map so array[word] -> # of occurrences, and iterate through word by word but I'm not sure how to do this using client side JavaScript.
I tried searching JavaScript word count but I only get results on counting the total number of words, which is not what I'm looking for. What I am looking for is more keeping a count of each specific word. Can this be done in Javascript or Jquery? If so, how should I go about doing this?
Here is an approach for you
// first get an array of words
var words = text.split(' ');
// use Array.prototype.reduce (for example) to
// create statistics
var statistics = words.reduce(function (stat, word) {
if (!stat[word]) stat[word] = 0;
stat[word]++;
return stat;
}, {});
UPDATE: A little example which handles punctuation and upper/lower case, too: http://jsfiddle.net/1pnLzv8h/1/
Something like
var arra = ['ab','pq','mn','ab','mn','ab']
function wordCount(arra,val)
{
var ob={};
var len=arra.length;
for(var k=0;k<len;k++)
{
if(ob.hasOwnProperty(arra[k]))
{
ob[arra[k]]++;
continue;
}
ob[arra[k]]=1;
}
return ob[val];
}
alert(wordCount(arra,'ab')); //output 3
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my page there are some article tags with an attribute data-course-id.
<article class="register" data-course-id="0123"></article>
<article class="register" data-course-id="0124"></article>
Now I would like to generate a variable which contains an array from the article tags and the value should be the same as the data-course-id.
var array = [0123, 0124]
How can I do this?
Like this:
var array = $('.register').map(function(){
return $(this).attr('data-course-id'); // converting to Number will result in loss
// of data since what you are using is octal representation.
}).get();
Without jQuery, you can try something like this:
var articles = document.getElementsByClassName("register");
var array = [];
for(var i = 0; i < articles.length; i++) {
array.push(articles[i].getAttribute("data-course-id"));
}
Working demo.
try like this,
$('.register').each(function(){
arr.push($(this).attr('data-course-id'));
alert($(this).attr('data-course-id'));
});
Live Demo Here..
Using jquery, I would do something like this
Pushing the data values:
var array=[]
$(".register").each(function(){
array.push($(this).data('course-id'));
});
alert("pushing only the data values:" + array)
Pushing the objects:
array2 = []
$(".register").each(function(){
array2.push($(this))
});
alert("pushing the objects: " + array2)
http://jsfiddle.net/x9LdR/1