React Javascript Array Object [closed] - javascript

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 5 years ago.
Improve this question
if I have a local constant like
result = [{id: 1, a:"1", b:"2"},{id:2, a:"3", b:"4"}]
my map function in react js works fine.
If I make a request to my database I get the same back, but as a string. But then the map function fails. I tried some Parse Function but didn't get it to work.
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb.db');
db.serialize(function() {
db.all(`SELECT * from List WHERE a LIKE '%${search}%' OR b LIKE '%${search}%'`,function(err,rows){
console.log(rows)
console.log('---------------------')
console.log(' ')
if (err) {
log(err)
res.status(400).send(err)
} else {
res.status(200).send(rows)
}
});
});
db.close();
Any idea?
Best regards

You need to parse that object with JSON.parse(string) first.

Related

How can i combinate push and update from an value in an array? [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
How can i combinate push and update event for an array?
i tried with assign but that was not working
$(`#saveall${roomname}`).on('click',function() {
var tisch = document.getElementById(`${roomname}_tisch`).value;
item = {};
item ["tisch"] = tisch;
itemsnew.push(item);
});
the Problem what i have if i update the value i got a new entry :
items: Array(2)
0: {tisch: "3"}
1: {tisch: "6"}
i will still update the value from tisch and not new entry in my array
{tisch: tisch} creates the same object in one expression; in fact, in ES6 you can simply use the shorthand {tisch}. Given this:
itemsnew.push({tisch});
You can do it like so:
$(`#saveall${roomname}`).on('click',function() {
var tisch = document.getElementById(`${roomname}_tisch`).value;
itemsnew.push({
tisch, // => this is equivalent to "tisch": tisch
/* add more properties if you need to */
});
});

How to parse or loop this [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 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

Where to store return messages for 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 5 years ago.
Improve this question
Where should I store message types javascript function is returning? I can just return string messages, but it seems like a bad idea. Are there any best practices?
For example in this kind of function (of course real functions make more sense then this)
function isFooValid(foo){
if(foo.length < 4){
return "TOO_SHORT";
} else if(foo.length >100) {
return "TOO_LONG";
} else if(foo.charAt(1) == 'A'){
return "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A";
}
}
You can create a variable for that. For example you can have:
myMessages ={
"secondChar": "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A",
"tooShort": "TOO_SHORT"
}
Then you can retrieve them as myMessages.tooShort, for example.
Even, if you want to have something similar to the string resources in android, you can store all the strings in a json file. If you load it before you js script, you are going to have the myMessages var available.

how to append to a file on particular position using node.js? [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 8 years ago.
Improve this question
I have a file and I want to append data to particular position on it.
I went through http://nodejs.org/api/fs.html#fs_fs_appendfile_filename_data_options_callback but I couldn't found to append data on specific position in file.
So any help would be appreciated.Thank you.!!
Refer this link
Using this you can write to a specific position in a file.
Code snippet:
var position = 5;
var file_path = 'file.txt';
var new_text = 'abcde';
fs.readFile(file_path, function read(err, data) {
if (err) {
throw err;
}
var file_content = data.toString();
file_content = file_content.substring(position);
var file = fs.openSync(file_path,'r+');
var bufferedText = new Buffer(new_text+file_content);
fs.writeSync(file, bufferedText, 0, bufferedText.length, position);
fs.close(file);
});
The file.txt should be on the same path.
file.txt already contains this text: OldText
Output: The new text will be OldTeabcdext i.e, OldTeabcdext

How to properly iterate through nested arrays 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 8 years ago.
Improve this question
I am having a hard time learning how to iterate through nested arrays, and storing specific array values in javascript.
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
console.log(res);
});
When #btn2 is clicked on, the following is outputed to my console:
If I'd like to access anyone of these values how would I do so? How do I store the value in a variable?
you can iterate through the object and get the value you want:
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
for(var i in res)
{
if (res.hasOwnProperty(i)) {
var obj = res[i];
console.log('name: ', obj.name);
console.log('sat: ', obj.sat);
}
}
});

Categories