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);
}
}
});
Related
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 */
});
});
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 4 years ago.
Improve this question
I have an array of objects which I would like to contain wildcards.
Is it possible to somehow implement wildcards in my objects' keys and allow this type of matching?
dict = [{"foo*" : "A" }, {"bar*" : "B"}]
if (dict["foo_1_2"]){
console.log("FOUND");
}
one way you can do is iterate over array get key of each object and test with regx
var key="foo_1_2";
var dict = [{"foo*" : "A" }, {"bar*" : "B"}]
function isKeyExists(key,array){
for(var i in array){
var regX = new RegExp('^'+Object.keys(array[i]),"g");
if(regX.test(key)){
return true;
}
}
return false;
}
console.log(isKeyExists(key,dict));
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
});
});
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.
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 8 years ago.
Improve this question
I got a key-value object like this:
items = {1:"a",2:"v",3:"u"};
But i want it like this.
items = [{"key":"1","value","a"},{"key":"2","value","v"},{"key":"3","value","u"}];
What is the best way to do this?
I have already tried to do this with $.each.
var items = {1:"a",2:"v",3:"u"};
var newItems = [];
$(items).each(function(k,v){
newItems.push({"key":k,"value":v});
});
And please tell me what is wrong with this question!
Try this-
var items = {1:"a",2:"v",3:"u"};
var obj=[];
for(var i in items){
obj.push({"key":i,"value":items[i]});
}
console.log(obj)