简体   繁体   English

如何使用javascript访问json对象0

[英]How do access a json object 0 using a javascript

I pretend to do console.log of the holidays variable appear in the list and not as an array. 我假装做了holiday.log的holidays变量出现在列表中,而不是作为数组。 How do I get this information as a simple object? 如何将此信息作为简单对象获取?

My json: 我的json:

[{
    "2016-10-10":[{"name":"Columbus Day","country":"US","date":"2016-10-10"}],
    "2016-10-31":[{"name":"Halloween","country":"US","date":"2016-10-31"}]
}]

My app: 我的应用:

app.factory('calendario', function($http) {    
    return{
        getJobs : function() {
            return $http({
                url: '/app/components/home/controller/test_calendar.json',
                method: 'GET'
            })
        }
    }     
}); 

controller: 控制器:

    var holidays = [];

    calendario.getJobs().success(function(data){
        holidays.push(data[0]);
    }); 

    $scope.setDayContent = function(date) {

        console.log(holidays);

}; 

Example: 例:

I want this 我要这个

在此输入图像描述

I don't want this 我不想要这个

在此输入图像描述

Instead of a list use a map 而不是列表使用地图

holidays = {}

Then put the items in the map with key the date and value the array: 然后将项目放在地图中,键上日期并为数组赋值:

holidays[data[0].date] = data[0].days;

But it looks like you already have the map, so probably you can just save data directly instead of pushing it in a list: 但看起来你已经拥有了地图,所以你可能只是直接保存数据而不是在列表中推送数据:

var holidays = {};

calendario.getJobs().success(function(data){
    holidays=data;
}); 

$scope.setDayContent = function(date) {
  console.log(holidays);
};

This question is to easy ... no? 这个问题很容易......不是吗?

    var holidays = [];

    calendario.getJobs().success(function(data){
        holidays=data[0];
    }); 

    $scope.setDayContent = function(date) {
      console.log(holidays);
    };

You want to flatten it, so, Nick is right, do not use the array, use dict/hash instead. 你想扁平它,所以,尼克是对的,不要使用数组,而是使用dict / hash。 In case your functionality will suffer you can use some workaround: 如果您的功能受到影响,您可以使用一些解决方法:

var holidays = [{
    "2016-10-10":[{"name":"Columbus Day","country":"US","date":"2016-10-10"}],
    "2016-10-31":[{"name":"Halloween","country":"US","date":"2016-10-31"}]
    }];
var flatten_holidays = holidays.reduce(function(elem){return elem});

I do not feel like it is specifically AngularJS question, more like common JavaScript instead. 我不觉得它是特定的AngularJS问题,更像是常见的JavaScript。 For your example it would be: 对于你的例子,它将是:

var holidays = [];

calendario.getJobs().success(function(data){
    holidays.push(data[0]);
}); 

$scope.setDayContent = function(date) {
    console.log(holidays.reduce(function(elem){return elem}));
};

or, if the preference is to use dict/hash: 或者,如果首选是使用dict / hash:

var holidays = {};

calendario.getJobs().success(function(data){ 
//if data is: {"2016-10-10":[{"name":"Columbus day","country":"US","date":"2016-10-10"}]}
var key = Object.keys(data)[0];
var value = data[key];
holidays[key] = value;
}); 

$scope.setDayContent = function(date) {
    console.log(holidays);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM