Console doesn't print key value [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 3 years ago.
Improve this question
This is my part of the JSON code. pants would be the "children" key of MALE if selected gender is male, while it would be children key of FEMALE if selected gender is female.
var data = {
"gender": "male",
"myFilter": {
"MALE": {
"pants": ["33", "34"]
},
"FEMALE": {}
}
}
console.log(myFilter.data[gender.value].pants[0]);
How do I do a clean work to do the job? I'd simply want to use the selected gender not as a key, but as a variable key. I tried everything but nothing works.
data is the full JSON payload.

var data = {
"gender": "male",
"myFilter": {
"MALE": {
"pants": ["33", "34"]
},
"FEMALE": {}
}
}
var pants = data.myFilter[data.gender.toUpperCase()].pants;
for(var i=0; i<pants.length; i++){
console.log(pants[i])
}

data.gender.toLowerCase() === 'male'
? data.myFilter.MALE.pants[0]
: data.myFilter.FEMALE.pants[0]
For Dynamic Keys
data.myFilter.[data.gender.toUpperCase()].pants[0]

IF your object is like this.
data: {
"gender": "male",
"myFilter": {
"MALE": {
"pants": ["33", "34"]
},
"FEMALE": {}
},
}
Then (gender value is maybe "MALE" or "FEMALE")
Console.log(data.myFilter[gender.value].pants[0]);

Try this and see if it works
console.log(data.myFilter[data.gender.toUpperCase()]["pants"][0]);

Related

(javascript) local storage value -> Can I change the order of the objects? [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
I set the data to a key called 'todo' in Local Storage.
The structure is like this.
key: todo
value : [{"text":"text1","idx":1, "complete":"Y"},
{"text":"text2","idx":2, "complete":"N"},
{"text":"text4","idx":4, "complete":"Y"}]
My to-do list can be deleted and changed between 'complete' and 'incomplete' status.
I want to change the order of to-do to the bottom when it is completed.
For example,
{"text": "text1", "idx": 1, "complete": "Y"}
When the "complete" value of this object has changed to "N".
({"text": "text1", "idx": 1, "complete": "N"})
I want this result.
key: todo
value : [{"text":"text4","idx":4, "complete":"Y"},
{"text":"text2","idx":2, "complete":"N"},
{"text":"text1","idx":1, "complete":"N"}]
The completed to-do wants to appear at the bottom.
Use custom sort function Array.prototype.sort(function(){})
let tasks = {
key: "todo",
value: [{
"text": "text1",
"idx": 1,
"complete": "Y"
},
{
"text": "text2",
"idx": 2,
"complete": "N"
},
{
"text": "text4",
"idx": 4,
"complete": "Y"
}
]
};
tasks.value.sort((a1, a2) => {
if (a1.complete == "Y") {
if (a2.complete == "Y")
return 0;
else
return -1;
} else {
if (a2.complete == "Y")
return 1;
else
return 0;
}
})
console.log(tasks);

Find specific key/value in array of objects and print out another key/value in that object [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 7 years ago.
Improve this question
I have this response from a $.getJSON call. Now I want to select the name property of the object with "selected": true, and print it to a div with id charTitle.
"titles": [{
"id": 17,
"name": "Sergeant %s"
}, {
"id": 53,
"name": "%s, Champion of the Naaru"
}, {
"id": 64,
"name": "%s, Hand of A'dal",
"selected": true
}]
You can achieve this with a simple loop:
for (var i = 0; i < obj.titles.length; i++) {
if (obj.titles[i].selected) {
$('#charTitle').text(obj.titles[i].name);
}
}
Example fiddle
Or with jQuery's $.each():
$.each(obj.titles, function(i, title) {
if (title.selected)
$('#charTitle').text(title.name);
});
Note that if you have multiple objects in the array with selected set to true you would need to use append() instead of text() to set the content of the div, otherwise the previous value will be overwritten.
using underscore you can get this with
var titles = ...
_.findWhere(titles, {selected: true});
see http://underscorejs.org/#findWhere
Try using Array.prototype.filter()
var arr = [{
"id": 17,
"name": "Sergeant %s"
}, {
"id": 53,
"name": "%s, Champion of the Naaru"
}, {
"id": 64,
"name": "%s, Hand of A'dal",
"selected": true
}]
var res = arr.filter(function(val, key) {
return val.selected === true
})[0].name;
$("#charTitle").html(res)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="charTitle"></div>

Display multiple same JSON keys using Angularjs [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 7 years ago.
Improve this question
When I type Monday and click Search I want to get all of Monday's JSON data and not just the first row. I've tried modifying the forEach function and also using two ng-repeats in html. So I guess it's the JSON Data structure?
Fiddle.
"Monday": [{
"Name": "John",
"Address": "Street",
"Phone": "111",
"Status": "Sleep"
},
{
"Name": "Sam",
"Address": "Street2",
"Phone": "2",
"Status": "Awake"
}
],
"Tuesday": [{..............................
You need to use this code:
angular.forEach($scope.items, function (value, key) {
if (key === enteredValue) {
angular.forEach(value, function(item) {
$scope.results.push({
name: key,
address: item.Address,
phone: item.Phone,
status: item.Status
});
});
}
});
The problem was that you only used one forEach, when you actually need two. Not a problem with your data structure.
You could also just do this, however:
angular.forEach($scope.items[enteredValue], function (item, key) {
$scope.results.push({
name: enteredValue,
address: item.Address,
phone: item.Phone,
status: item.Status
});
});
which would be must simpler. I hope this helps!

How to convert JSON Array to HTML select (with checkboxes)? [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 an array of json objects
[
{
"id": 1,
"name": "Max"
},
{
"id": 2,
"name": "Bob"
},
{
"id": 3,
"name": "Mike"
}
]
How to convert it to html select (with checkboxes)?
Here's a simple example that might help you:
<select name="datas" id="datas"></select>
<script>
html = "";
obj = {
"1" : "Name",
"2": "Age",
"3" : "Gender"
}
for(var key in obj) {
html += "<option value=" + key + ">" +obj[key] + "</option>"
}
document.getElementById("datas").innerHTML = html;
</script>
Hope this helps.
Keep me posted

call javascript object properties [closed]

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
This is probably a very easy question but let's say I have a php json_encoded javascript object of this form:
var js_objet = {
"1": {
"Users": {
"id": "14",
"name": "Peter"
},
"Children": [
{
id: 17,
name: "Paul"
},
{
id: 18,
name: "Mathew"
}
]
}
}
What is the synthax to get the name of the child with id 18 (Mathew) for example from that Object?
Thank you
Here's such a custom function as mentioned earlier.
function getChildrenById(id){
for (var child in js_objet[1].Children){
if(id == js_objet[1].Children[child].id){
return js_objet[1].Children[child].name;
}
}
return false;
}
running getChildrenById(18) would return "Mathew"
Hope that helps,
R.
You can do it like this:
var name = js_objet["1"].Children[1].name;
JSFiddle available here
This is how I would approach this problem:
var id = 18;
var name = '';
var jsObjIndex = "1";
var jsObjAttr = "Children"
for (var i = 0; i < js_objet[jsObjIndex][jsObjAttr].length; i++)
{
if (js_objet[jsObjIndex][jsObjAttr][i].id == id)
{
name = js_objet[jsObjIndex][jsObjAttr][i].name;
break;
}
}
You are basically performing a linear search on every element of jsObjIndex.Children. There are likely better solutions however :).
This really is a comment, but I don't have enough reputation (but it's relevant as I recently encountered the same issue): This would probably be easier to do if you can change the JSON format to use the ID as the key for each child object:
var js_objet = {
"1": {
"Users": {
"id": "14",
"name": "Peter"
},
"Children": {
"17":{
"name": "Paul"
},
"18":{
"name": "Mathew"
}
}
}
}
Then you can simply call it using
js_objet[1].Children[18].name;
This is assuming each ID is unique.

Categories