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
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 last month.
Improve this question
I have an array of words in a .json file
Like this:
[
"city",
"river",
"car"
]
And i want to get an object for each word
Something like this:
[
{
word: "city",
something: "..."
},
{
word: "river",
something: "..."
},
{
word: "car",
something: "..."
}
]
What is the best way to do this?
let new_array = [];
for(let item of old_array)
{
new_array.push({word:item, something:"..."});
}
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have json object like this
[
{
"tag": "search"
},
{
"tag": "test"
},
{
"tag": "css"
},
]
But i want to get this object like this
'search',
'test',
'css',
How do I do this?
You can map over the array and extract the tag property value:
const obj = [ { "tag": "search" }, { "tag": "test" }, { "tag": "css" }, ]
const res = obj.map(e=>e.tag)
console.log(res)
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);
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>
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.