I am working in an AngularJS controller, trying to get the current month's name, this is my code:
$scope.months = {};
$scope.currentMonth = new Date().getMonth() + 1;
$http.get("json/months.json").success(function (data) {
$scope.months = data;
});
console.log($scope.months);
$scope.currentMonthName = $scope.months[0].name;
//Rest of file
This is my JSON file:
[
{"id": "0", "name": "January" },
{"id": "1", "name": "February"},
{"id": "2", "name": "March"},
{"id": "3", "name": "April"},
{"id": "4", "name": "May"},
{"id": "5", "name": "June"},
{"id": "6", "name": "July"},
{"id": "7", "name": "August"},
{"id": "8", "name": "September"},
{"id": "9", "name": "October"},
{"id": "10", "name": "November"},
{"id": "11", "name": "December"}
]
However my $scope.currentMonth is throwing an error 'TypeError: Cannot read property 'name' of undefined'. What I make of this is that he did not even retrieve the JSON file correctly (undefined), however after logging this file to console I can see that he did retrieve it correctly, which leaves me to wonder what I did wrong?
The $http query is asynchronous.
So try this (you need to set the assignment in the query callback) :
$scope.months = {};
$scope.currentMonth = new Date().getMonth() + 1;
$http.get("json/months.json").success(function (data) {
$scope.months = data;
console.log($scope.months);
$scope.currentMonthName = $scope.months[0].name;
});
I think you'll need to parse it before you use it.
$http.get("json/months.json").success(function (data) {
data = JSON.parse(data);
$scope.months = data;
});
JSON is just a string. Use JSON.parse("json") to convert it to an actual object.
Related
I am getting http response like this:
[
{"id": "1", "name": "2", "value": "3", "any": "4"}
]
I want to convert it to something like this:
[
{
"heading": "id"
"content": "1"
},
{
"heading": "name"
"content": 2
},
{
"heading": "value"
"content": 3
},
{
"heading": "any"
"content": 4
}
]
I am using angular4.0.0 and I want to perform this in service method.
How to achieve this result?
Here you go :
var arrayData = [
{"id": "1", "name": "2", "value": "3", "any": "4"}
]
let finalArray = arrayData.map(el => {
let returnArray = [];
for(let key in el){
returnArray.push({heading : key , content : el[key]})
}
return returnArray;
})
console.log(finalArray);
var response = [
{"id": "1", "name": "2", "value": "3", "any": "4"}
];
var newJson = [];
response.forEach(function(val,index){
Object.keys(val).forEach(function(data) {
newJson.push({heading: data,content:val[data]})
})
console.log(newJson)
})
var responseData=[
{"id": "1", "name": "2", "value": "3", "any": "4"}
];
var finalResult = [];
responseData.map(function(item){
var test = [];
var allKeys=Object.keys(item);
for(i=0;i<allKeys.length;i++)
{
finalResult.push({'heading':allKeys[i],'content':item[allKeys[i]]});
}
});
console.log(finalResult)
I have external JSON file call datas. This is the body of that JSON file.
[
{"value": "1", "text": "aaa"},
{"value": "2", "text": "bbb"},
{"value": "3", "text": "ccc"},
{"value": "4", "text": "ddd"},
{"value": "5", "text": "eee"},
{"value": "6", "text": "fff"},
{"value": "7", "text": "ggg"},
{"value": "8", "text": "hhh"},
{"value": "9", "text": "iii"},
{"value": "10", "text": "jjj"}
]
I want to filter data from this JSON file according to following array "b" values.(b0, b1, b3 etc)
$scope.array = {"badge":"1,2,5,7","id":"0","b0":"1","b1":"2","b2":"5","b3":"7"}
Example:
This array have b0, b1, b2 and b3 those values are 1, 2, 5 and 7. Therefor I want to get only 1, 2, 5, 7 values arrays from datas JSON file and display text values of this array.
This array can be change with same format. Therefor I want to consider b+"number" parameters.
Example 1:
$scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"}
Example 2:
$scope.array = {"badge":"1,2,7","id":"0","b0":"1","b1":"2","b2":"7"}
Example 3:
$scope.array = {"badge":"1,2,5,7,8,9","id":"0","b0":"1","b1":"2","b2":"5","b3":"7","b4":"8","b5":"9"}
I get that JSON external file using angularjs like this,
$http.get("/json/datas.json").success(function(data) {
$scope.datas= data;
});
Values are display using repeat.
<div ng-repeat="data in datas">
<span ng-bind-html="data.text"></span>
</div>
Display HTML body only
aaa
bbb
eee
ggg
One way to do it is to filter, map, and/or reduce the array that has the "bX" values to create a lookup table of IDs, then filter the main data array based on that lookup table. Except that that "array" isn't an array, it is a plain object, so you can't use array methods on it directly. So I'm calling Object.keys() to get its keys in an array, and then I've chosen to use .reduce() to create the lookup table based on the keys that have the right format:
var data = [ {"value": "1", "text": "aaa"}, {"value": "2", "text": "bbb"}, {"value": "3", "text": "ccc"}, {"value": "4", "text": "ddd"}, {"value": "5", "text": "eee"}, {"value": "6", "text": "fff"}, {"value": "7", "text": "ggg"}, {"value": "8", "text": "hhh"}, {"value": "9", "text": "iii"}, {"value": "10", "text": "jjj"} ]
var $scope = {} // demo $scope object
$scope.array = {"badge":"1,2,5,7","id":"0","b0":"1","b1":"2","b2":"5","b3":"7"}
var bVals = Object.keys($scope.array).reduce(function(a, c) {
if (/^b\d+$/.test(c))
a[$scope.array[c]] = true
return a
}, {})
console.log(bVals)
var filteredData = data.filter(function(v) { return bVals[v.value] })
console.log(filteredData)
You can use javascript prototype functions map and find to filter the data.
First get the batch properties to an array and map the array to find the relevant values
$scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"}
var batchArr = $scope.array.badge.split(',');
$scope.result = batchArr.map(o=> $scope.datas.find(k=> k.value == o))
angular.module("app",[])
.controller("ctrl",function($scope,$sce){
$scope.datas = [
{"value": "1", "text": "aaa"},
{"value": "2", "text": "bbb"},
{"value": "3", "text": "ccc"},
{"value": "4", "text": "ddd"},
{"value": "5", "text": "eee"},
{"value": "6", "text": "fff"},
{"value": "7", "text": "ggg"},
{"value": "8", "text": "hhh"},
{"value": "9", "text": "iii"},
{"value": "10", "text": "jjj"}
]
$scope.array = {"badge":"1,2,3,9","id":"0","b0":"1","b1":"2","b2":"3","b3":"9"}
var batchArr = $scope.array.badge.split(',');
$scope.result = batchArr.map(o=> $scope.datas.find(k=> k.value == o))
console.log($scope.result)
$scope.trust = function(html){
return $sce.trustAsHtml(html);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="data in result">
<span ng-bind-html="trust(data.text)"></span>
</div>
</div>
I would like to filter my JSON array according to JSON array. I have following JSON array.
$scope.datas = [
{"value": "1", "text": "aaa"},
{"value": "2", "text": "bbb"},
{"value": "3", "text": "ccc"},
{"value": "4", "text": "ddd"},
{"value": "5", "text": "eee"},
{"value": "6", "text": "fff"},
{"value": "7", "text": "ggg"},
{"value": "8", "text": "hhh"},
{"value": "9", "text": "iii"},
{"value": "10", "text": "jjj"}
];
I want to filter this data according to my next JSON array.
$scope.array = {"badge":"1,2,3,9"};
This array can be change with same format.
Example 1:
$scope.array = {"badge":"1,2,3,9"}
Example 2:
$scope.array = {"badge":"1,2,7"}
Example 3:
$scope.array = {"badge":"1,2,5,7,8,9"}
I want to filter datas JSON array according to array of badge.
Display HTML body only
aaa bbb eee ggg
My code is working but older IE are not working because of it has find function.
$http.get("datas.json").success(function(data) {
$scope.spitedata = response.data.badge.split(',');
$scope.badgers = $scope.spitedata.map(function(o){ return data.find(function(k){return k.value == o})});
});
How can I get the id of info1 or info2 with each information of the inside loop by jquery loop. Fo example I want to get id 1 from info1 then all the information within id 1 similarly same as from info2. I need the output to show in the browser.
var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080#yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080#yahoo.com",
"clientusername": "kaosar"
}
},
"info2": {
"3": {
"id": "24",
"receiver": "5",
"content": "chat system",
"time": "2015-08-19 12:09:19"
},
"4": {
"id": "23",
"receiver": "4",
"content": "chat system",
"time": "2015-08-19 12:09:19"
}
},
}
Thanks in advance.
Iterate the json array and access the object like the following code.
var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080#yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080#yahoo.com",
"clientusername": "kaosar"
}
},
"info2": {
"3": {
"id": "24",
"receiver": "5",
"content": "chat system",
"time": "2015-08-19 12:09:19"
},
"4": {
"id": "23",
"receiver": "4",
"content": "chat system",
"time": "2015-08-19 12:09:19"
}
},
};
for(var j in data){
for(var k in data[j]){
console.log(data[j][k]);
}
}
Your browser's Console will log the following objects if you run the above example.
Object {clientname: "ruhul yahoo", clientemail: "ruhul080#yahoo.com", clientusername: "ruhulya"}
Object {clientname: "kaosar yahoo", clientemail: "kaosar080#yahoo.com", clientusername: "kaosar"}
Object {id: "24", receiver: "5", content: "chat system", time: "2015-08-19 12:09:19"}
Object {id: "23", receiver: "4", content: "chat system", time: "2015-08-19 12:09:19"}
Then you can access the values like a normal object console.log(data[j][k].clientname);
This function will find you the first instance of a variable name in the object. If you need to find a variable in a specific path you could amend this function fairly easily to do that. Certainly the function as is passes the test case you've provided.
function findVar(data, varName) {
for (var i in data) {
if (i === varName) return data[i];
if (typeof (data[i]) === 'object') {
var findResult = findVar(data[i], varName)
if (typeof(findResult) !== 'undefined')
{
return findResult;
}
}
}
return undefined;
}
Firstly, This is not a valid JSON, Rmove the last , before last {
Secondly , parse it as a JSON and get the info as
data.info1[1].clientname
var data = JSON.parse('{"info1":{"1":{"clientname":"ruhul yahoo","clientemail":"ruhul080#yahoo.com","clientusername":"ruhulya"},"2":{"clientname":"kaosar yahoo","clientemail":"kaosar080#yahoo.com","clientusername":"kaosar"}},"info2":{"3":{"id":"24","receiver":"5","content":"chat system","time":"2015-08-19 12:09:19"},"4":{"id":"23","receiver":"4","content":"chat system","time":"2015-08-19 12:09:19"}}}');
alert(data.info1[1].clientname);
alert(data.info1[2].clientname);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
You can iterate over this object like this
for(var i in data){
for(var j in data[i]){
console.log(data[i][j]);
}
}
I have an array of object like this
[
{"id": "1", "name": "test"},
{"id": "2", "name": "test2"},
{"id": "3", "name": "test3"}
]
I want to convert it to object list this
{
"1": {"name": "test"},
"2": {"name": "test2"},
"3": {"name": "test3"},
}
You may use reduce :
var obj = arr.reduce(function(m,o){ m[o.id]={name:o.name}; return m }, {});
Side note : please be sure to read and try to understand T.J.'s comment about JSON