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!
Related
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 6 months ago.
Improve this question
I want to convert this:
data: {
"3": {
name: ["Missing data for required field."],
},
"5": {
id: ["Same id exist."],
},
}
into this:
data: [{
key: "name",
message: "Missing data for required field.",
row: 3,
},
{
key: "id",
message: "Same id exist.",
row: 5,
},]
how to achieve this with just es6 syntax
You can do this with Object.entries:
const data = {
"3": {
name: ["Missing data for required field."]
},
"5": {
id: ["Same id exist."]
}
};
Object.entries(data).map(([a, b]) => {
return {
key: Object.keys(b)[0],
message: Object.values(b)[0][0],
row: a
};
});
I've made some assumptions:
There will only be one key in each object
The value of the key will be an array with one string
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 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]);
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 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 7 years ago.
Improve this question
Following JSON data comes from php,
I have tried following code. But console displays undefined error.
jQuery.each(result.address, function(obj) {
console.log(obj.city);
});
EDIT : Also it's not working and throwing undefined error.
jQuery.each(result.address, function(obj) {
console.log(obj[0].city);
});
It is working : console.log(result.address.address1.city);. But in this case address1 is not fixed. eg. result.address.xyz.city, result.address.abc.city
You're calling city on each address, while cities are inside addressX inside address. Try:
jQuery.each(result.address, function(key, val) {
console.log(val.city);
});
jsfiddle DEMO
Maybe in this way:
for (i = 0; i < result.address.length; i++) {
console.log(result.address[i].city);
}
I'm not sure if jquery.each works in this case.
I hope it helps.
Javascript
var dictionary = {
"12Jan2013": [{
"id": "0",
"name": "ABC"
}, {
"id": "1",
"name": "DEF"
}],
"13Jan2013": [{
"id": "0",
"name": "PQR"
}, {
"id": "1",
"name": "xyz"
}]
};
for (var i in dictionary) {
dictionary[i].forEach(function(elem, index) {
console.log(elem, index);
});
}
Output
Object {id: "0", name: "ABC"} 0
Object {id: "1", name: "DEF"} 1
Object {id: "0", name: "PQR"} 0
Object {id: "1", name: "xyz"} 1