Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
let people = ['John','Sally','Jake','Chris'];
const logPerson = (person, index) => {
console.log('${index} - Hello ${person}');
};
people.forEach(logPerson);
it keeps printing out like this
"${index} - Hello ${person}"
how do I get it to display the names & index value code looks right as far as I can tell and from what I searched on google.
let people = ['John','Sally','Jake','Chris'];
const logPerson = (person, index) => {
console.log(`${index} - Hello ${person}`);
};
people.forEach(logPerson);
You need to use the BACKTICK, not the regular quote.
console.log(`${index} - Hello ${person}`);
If you are on the US keyboard, that symbol is just above the tab key, right beside the 1 key.
const logPerson = (person, index) => {
console.log(`${index} - Hello ${person}`);
};
people.forEach(logPerson);
In order to reference variables like this you need to use a backquote. However, if you are going to use regular quotes, you can use commas within your string if that is easier:
const logPerson = (person, index) => {
console.log(index, "- Hello", person);
};
The code above will give you the same output. You can also use the plus operator. Whichever form you choose is up to you, but use the backquote when you are trying to reference variables the way you need. Use normal quotes when attempting to reference variables normally and concatenating them within a string using the + or , operators.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
On a React Page inside render I have the code below, which produces the error:
topic.language.map is not a function
{console.log(JSON.stringify(topic.language))} //this returns "English,French,Other"
{topic.language.map(
(ln, i) => {
return (
<div
key={i}
className="language"
>
{ln}
</div>
);
}
)}
What am I doing wrong and how can I map the different languages? Do I need .split instead of .map? What would that look like?
If your language value is a string, you will need to split the string into an array before calling map.
const topic = { language: 'English,French,Other' };
topic.language.split(/,/g).map((ln, i) => {
console.log(ln);
});
//assuming your string is like "part1,part2,part3......partn"
//you can just use
//this will return an array of string spitted by your delimiter
//here is ',' comma
//put that is split method and you will be good to go
let stringSplitArray = "part1,part2,part3,part4".split(",")
//now you can do
stringSplitArray.map(element=>(
<div>{element}</div>
))
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 have keyword (for example -2330) that needs to be compared with the 'values' in this string below.
"[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]";
Expected output needs to be true or false depending if the string has the keyword or not.
How do I check it?
I will do something like this to loop
var a = "[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]";
var new_a = JSON.parse(a);
var value_compare = '1574';
new_a[0]['value'].forEach(element => {
if (element[0].value == value_compare) {
//DO SOMETHING
alert('found: '+ JSON.stringify(element));
}
});
You first need to parse the JSON into an suitable JS structure. Because of the nested nature of the data you need to 1) map over the first object in your array, 2) return the value of the value property of the first object of each and finally 3) check to see if the keyword is included in the the returned array, and return true or false.
const json = '[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]';
const data = JSON.parse(json);
function doesItExist(data, keyword) {
const values = data[0].value.map(arr => arr[0].value);
return values.includes(keyword);
}
console.log(doesItExist(data, '2331'));
console.log(doesItExist(data, 'Bob'));
console.log(doesItExist(data, '1574'));
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
So, hello. I edited the entire thing.
app.get('/', async (req, res) => {
let results = await db.collection("malwarepad-website").find("6047667ff156cb8135bdaa88").toArray()
//var resultsConverted = results.toString();
//let resultsFinal = resultsConverted.split('"');
console.log(results)
res.render('index.ejs', { startText: results });
})
In the above code I want to only keep the second part of it specified better in this image: https://i.stack.imgur.com/Wi031.png
I want to create a variable containing the following:
Hello, and welcome to my website. I don't know how you found me but yo...
I already have a constant containing the search results, but it is this:
[
{
_id: 6047667ff156cb8135bdaa88,
mainPage: "Hello, and welcome to my website. I don't know how you found me but you're welcome :)."
}
]
Thanks for the understanding :)
a = a.split("\"")[1]
If you mean extracting what's inside double quotations, you have two methods:
1 - Use Regular Expressions:
You can use regular expression /.*"(.*)".*/ which tries to capture everything inside parentheses. You can use exec method. like :
const importantPart = /.*"(.*)".*/.exec(a)[1] (a is your variable)
2 - Using indexOf string methods
In JavaScript strings have two useful methods: indexOf and lastIndexOf. In addition to a substring.
You can use these to extract the important part:
a.substring(a.indexOf('"') + 1, a.lastIndexOf('"'))
There are several solutions. One could be:
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let a_splitted = a.split('"');
console.log(a_splitted[1]);
You can use regular expressions to extract the part that you need.
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let result = a.match(/\"(.*)\"/);
console.log(result[1]);
There are a lot of what-ifs though.
const a = 'odshniudfskdjnfdsjnf"Important part"fererferferef';
let regex = /(?<=\")(.*?)(?=\")/;
let result = regex.exec(a)[0];
console.log(result);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i am using a javascript to get data and put it into a table, i dont know where i have gone wrong? please help!
my code is:
function getSOCsForJobTitle() {
var searchtitle = s("#search-input").val();
var apiurl = "http://api.lmiforall.org.uk/api/v1/soc/search?q="
var apicall = apiurl + searchtitle;
s.get(apicall.function (data) s.each(data.function (i.e) {
var tablerow = s("<tr></tr>");
tablerow.append("<td>" + e.title + "/td>");
tablerow.append("<td>" + e.SOC + "/td>");
s("#SOCstable").append(tablerow);
});
});
}
s(function () {
// this gets called when the page loads
s("#search-go") onclick(getSOCsForJobTitle);
});
apicall.function (data) s.each(data.function (i.e) {
First:
apicall is a string, so it won't have a function property. Presumably you are trying to pass it and the function as separate arguments to s.get. You need a comma, not a period.
apicall, function (data) s.each(data.function (i.e) {
Second:
It looks like you are trying to pass an anonymous function as the second argument. The syntax for that is:
function (arg) {
// expressions
}
You are missing the {.
apicall, function (data) {
s.each(data.function (i.e) {
Third:
It looks like you've made exactly the same error for what you are trying to pass to s.each.
s --> $
s("#search-go") onclick --> $("#search-go").click
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
How can I convert the following string using JavaScript to array:
from: var x = "{id:'2'},{name:'code,Barer'}";
to: var x1 = [{id:"2"},{name:"code,Barer"}];
If you want that exact string to be an array object you can do this:
var x = "{id:'2'},{name:'code,Barer'}";
var newArray = eval("[" + x + "]");
Here are some the dangers of eval: Why is using the JavaScript eval function a bad idea?
How are you getting the variable x? If you can get whatever it is to spit out valid JSON (using JSON.stringify or something similar) you can then parse it correctly into JS (although some implementations of JSON parsers do use eval).
If you want to avoid using eval for security reasons try this
var string = "{id:'2'},{name:'code,Barer'}",
array = string.substr(1, string.length - 2)
.split("},{")
.map(function(item){
item = item.split(":");
var result = {},
name = item[0],
value = item[1].replace(/'/g, "");
result[name] = value;
return result
});