You can use .includes:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.includes(thing)) console.log('hurray');
Note that this is entirely separate from Javascript's in operator, which checks for if something is a property of an object:
const obj = { foo: 'bar'}
const thing = 'foo';
if (thing in obj) console.log('yup');
Note that includes is from ES6 - make sure to include a polyfill. If you can't do that, you can use the old, less semantic indexOf:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.indexOf(thing) !== -1) console.log('hurray');
Answer from CertainPerformance on Stack OverflowI've been recently watching some Python videos and had the idea of making a simple cheatsheet comparing how to do the same basic things between two languages.
The cheatsheet assumes you are experienced in one of the languages and are looking to switch to the other and just need a quick reference. It doesn't try to teach you how to write either language because that's not the point.
https://www.theyurig.com/blog/javascript-python-syntax
I'm mainly just looking for someone with decent Python experience and who has the bare minimum Javascript experience (this is what I'm strong with) to help me build it and make it more robust. Because I want to offer a way to help with the least friction possible, you can just make a comment on the related issue on my repo and I'll add it.
Just be aware that I'm not gonna push change by change, but in bulks, so if you suggest something and it doesn't go live shortly afterward, it's most likely because I'm waiting to see if no more feedback is coming before I commit my current changes.
Post there since r/python is private due to protesting. Also, let me know if this specific request breaks rule #3 because I feel like it doesn't (since I'm creating a public resource) but I might just be wrong.
Do JavaScript arrays have an equivalent of Python’s “if a in list”? - Stack Overflow
What is the python equivalence of javascript ${variable}?
node.js - Python's %s equivalent in Javascript? - Stack Overflow
Is there a JavaScript equivalent to Python's for loops? - Stack Overflow
Videos
You can use .includes:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.includes(thing)) console.log('hurray');
Note that this is entirely separate from Javascript's in operator, which checks for if something is a property of an object:
const obj = { foo: 'bar'}
const thing = 'foo';
if (thing in obj) console.log('yup');
Note that includes is from ES6 - make sure to include a polyfill. If you can't do that, you can use the old, less semantic indexOf:
const good = ['beer', 'pizza', 'sushi']
const thing = 'pizza';
if (good.indexOf(thing) !== -1) console.log('hurray');
If you're using JavaScript ES6, I believe that Array.includes is what you're looking for. If you need support for older versions of JS, then you can use Array.indexOf (or use a polyfill for Array.contains).
// Array.includes()
if (yourArray.includes("some value")) {
// Do stuff
}
// Array.indexOf()
if (yourArray.indexOf("some value") > -1) {
// Do stuff
}
Since ES7, it is recommended to use includes() instead of the clunky indexOf().
var my_array = ['a', 'b', 'c'];
my_array.includes('a'); // true
my_array.includes('dd'); // false
var my_array = ['a', 'b', 'c'];
alert(my_array.indexOf('b'));
alert(my_array.indexOf('dd'));
if element not found, you will receive -1
I wish to do something like
cur.copy_from(source_file, whole_database_name, sep="|", null="")
I wanted to build a function over that and when there is a need for the columns attribute, it can be
cur.copy_from(source_file, whole_database_name, sep="|", null="" ${columns})
thanks
Use template literal. This is comparatively new and may not support ancient browsers
var test = "Dan"
var j = {
"dynamicText": `Hello ${test}, how are you?`
}
console.log(j["dynamicText"])
Alternatively you can create a function and inside that function use string.replace method to to replace a word with new word
var test = "Dan"
var j = {
"dynamicText": "Hello %s, how are you?"
}
function replace(toReplaceText, replaceWith) {
let objText = j["dynamicText"].replace(toReplaceText, replaceWith);
return objText;
}
console.log(replace('%s', test))
There is no predefined way in JavaScript, but you could still achieve something like below. Which I have done in my existing Application.
function formatString(str, ...params) {
for (let i = 0; i < params.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
str = str.replace(reg, params[i]);
}
return str;
}
now formatString('You have {0} cars and {1} bikes', 'two', 'three') returns 'You have two cars and three bikes'
In this way if {0} repeats in String it replaces all.
like formatString('You have {0} cars, {1} bikes and {0} jeeps', 'two', 'three') to "You have two cars, three bikes and two jeeps"
Hope this helps.
In the next version of ECMAScript (ECMAScript6 aka Harmony) will be for-of construct:
for (let word of ["one", "two", "three"]) {
alert(word);
}
for-of could be used to iterate over various objects, Arrays, Maps, Sets and custom iterable objects. In that sense it's very close to Python's for-in.
for an array the most similar is the forEach loop (of course index is optional)
[1,2,3,4,].forEach(function(value,index){
console.log(value);
console.log(index);
});
So you will get the following output:
1
0
2
1
3
2
4
3