Outputting an array in alphatabetical order [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wondering if I have the below:
var mycars = new Array();
mycars[0] = "Manbearpigs";
mycars[1] = "Cool";
mycars[2] = "Coolz";
mycars[3] = "Radical";
mycars[4] = "GiantCools";
What is the best way I could output it in alphabetical order in the HTML.
Eg:
Cool
Coolz
GiantCools
Manbearpigs

Just Sort it
var mycars = new Array();
mycars[0] = "Manbearpigs";
mycars[1] = "Cool";
mycars[2] = "Coolz";
mycars[3] = "Radical";
mycars[4] = "GiantCools";
mycars.sort();
Array Sort

You can use sort method
so your code would be mycars.sort();

Use can use array.sort() method to sort your array:
mycars.sort();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

You can use Underscore Library, which the best one for operating data in JavaScript

Related

Is there any function which adds 1 to the array element? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 11 months ago.
Improve this question
I have a given array like this let arr1 = ['a','b','c'];
And I want output like this ['a1','b1','c1']
Which function should I use?
For this task, the map function is the best candidate. It generates a new array by applying the function given as argument to each element.
let arr1 = ['a','b','c'];
console.log(arr1.map(x=>`${x}1`));

How to get the array of strings that is in between curly braces inside source string in JavaScript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Suppose the JavaScript variable is:
var sourceString="stack {overflow} is the best {place} to clear {technical} doubts";
Output javascript string array should contain: overflow,place,technical
or {overflow},{place},{technical}
I am fine with both the results.
You could use regex to accomplish this:
sourceString.match(/{.*?}/g)
var getMatchingGroups = function(s) {
var r=/\{(.*?)\}/g, a=[], m;
while (m = r.exec(s)) {
a.push(m[1]);
}
return a;
};
getMatchingGroups(sourceString) // ["overflow", "place", "technical"]

Converting a file to array with FS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple "en.txt"
"TITLE" => "Amazing title of my page"
"COPYRIGHT" => "Copyright my site"
"BLABLA" => "A amazing sentence"
And I would like convert this in array for nodeJS with FS.
Thanks for your help.
First, read the file data using the fs.readFile method. Once you have the file data in a variable, you can convert it into an array using the following regular expression:
var regex = /"([^"]+)"\s*=>\s*"([^"]+)/g;
var match, results = {};
while((match = regex.exec(fileData)) !== null){
results[match[1]] = match[2];
}
console.log(results); // contains js array of data
See Fiddle.

PHP alternative of Javascript's pop() method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there any PHP method to do:
var filename = $_POST["filename"];
filename = filename.split(".").pop();
How can I do the same above thing in PHP?
Something like this, perhaps?
$filetype = array_pop(explode('.',$filename));
It will generate an Only variables should be passed by reference notice. If you want to get rid of that, you'd need:
$fileparts = explode('.',$filename);
$filetype = array_pop($fileparts);
However, the best way to get a file's extension is by using pathinfo():
$filetype = pathinfo($filename, PATHINFO_EXTENSION);
You can do unset($filename[count($filename) - 1]);. Which will remove the last element in the array.
You'd want to do this:
$filename = $_POST['filename'];
$file_arr = explode(".",$filename); // may need to escape '.' here, can't remember
$filename = array_pop($file_arr);
In PHP, you probably don't need to convert to an array and pop; get the position of the last character, then get the rest of the string. From http://davidwalsh.name/php-file-extension:
substr(strrchr($file_name,'.'),1);

How to replace occurrences by key [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This is a simple question about JavaScript,
Say I got the following string:
A)My Name B)My Name C)My Name
now I give to a function the key #1 expecting to replace the second occurrence of My Name inside that function and return:
A)My Name B) C)My Name
I haven't found the solution to this anywhere online so I'm asking.
You could use split to separate the string into the parts, then join the before and after back together:
function removeNthMatch(input, removeString, removeIndex) {
var splitString = input.split(removeString);
result = splitString.slice(0, removeIndex + 1).join(removeString)
+ splitString.slice(removeIndex + 1).join(removeString);
}
input = "A)My Name B)My Name C)My Name";
removeString = "My Name";
removeIndex = 1;
console.log(removeNthMatch(input, removeString, removeIndex));

Categories

Resources