Quoting a list of comma separated string literals? [closed] - javascript

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 5 years ago.
Improve this question
I have a list of literals like this (These are unquoted strings - not variables):
alice-blue, antique-white, aqua, ...
And I want them to be quoted like this (For use in a nunjucks loop):
'alice-blue', 'antique-white', 'aqua', ...
Look for a simple way to do this in javascript.

var str = "alice-blue, antique-white, aqua";
var arr = str.split(', ');
for(var i=0; i< arr.length;i++){
arr[i] = "'"+arr[i]+"'";
}
var resultStr = arr.join(", ");

Related

I want to convert array to object in java script like[ ["key1","ans1"],["key2","ans2"] ]=> {key1:"ans1",key2:"ans2"} [closed]

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
I want to convert array to object in javascript like
[["key1","ans1"],["key2","ans2"]] => {key1:"ans1",key2:"ans2"}
Here you go;
let res = {}
let arr = [["key1","ans1"],["key2","ans2"]]
for (let i = 0; i < arr.length; i++) {
res[arr[i][0]] = arr[i][1]
}
console.log(res)
please comment if you need explanation.

Horizontal concatenation of two array [closed]

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
how to do horizontal concatenation of two arrays in javascript?
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
I want output like :
c = [[1,2,7], [3,4,8], [5,6,9]]
You can use map and use index to access corresponding value from second array
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
let final = a.map((v,i)=> v.concat(b[i]))
console.log(final)

How to generate set strings in javascript [closed]

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 3 years ago.
Improve this question
Trying to generate a set of strings such as 'TCP', 'DNS' or 'SQL'
I want to be able to have these strings set and be able to have them appear randomly like you would with random numbers.
You can make an array and select them randomly:
const strings = ['TCP', 'DNS', 'SQL'];
for (var i = 0; i < 5; i++) {
console.log(`Random string: ${strings[Math.floor(Math.random() * strings.length)]}`);
}

Which is preferred for typecasting to a String in JavaScript? String() or template literal? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Let's say I have some variable that is probably a number.
Is it preferred to use String() or use a template literal to convert this variable to a string?
const n = 3.14;
let strN = String(n);
// OR
strN = `${n}`;
or does it not make any difference other than the obvious readability issues?
airbnb's styleguide suggests String(n).
// => this.reviewScore = 9;
// good
const totalScore = String(this.reviewScore);
airbnb Coercion: strings
You Don't Know JS - Types & Grammar: Coercion

How i can filter some words of a statement with javascript? [closed]

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 9 years ago.
Improve this question
I wanna filter some words of a statement with javascript.
I have words must be filtered and words must be replaced :
var words = [
{'badword':/dog/, 'goodword':'1'},
{'badword':/gav/ , 'goodword':'5'},
{'badword':/folan/ , 'goodword':':6'}
// .
//.
//.
//and more
];
For Example I wanna /dog/ filter and 1 replaced.
How i can?
http://jsfiddle.net/billymoon/RF7Gm/1/
var text = "this is a dog called gav and a thing which is a folan here.";
var words = [
{'badword':/dog/, 'goodword':'1'},
{'badword':/gav/ , 'goodword':'5'},
{'badword':/folan/ , 'goodword':':6'}
];
for(var i=0;i<words.length;i++){
var word = words[i];
text = text.replace(word.badword, word.goodword);
};
alert(text);
Should output something like... this is a 1 called 5 and a thing which is a :6 here.

Categories

Resources