添加到JSON属性,可能或可能不存在尚未 [英] adding to json property that may or may not exist yet

查看:94
本文介绍了添加到JSON属性,可能或可能不存在尚未的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说,我想这样做。

  var dasboard = {};
  var page = "index";

  $('.check').click(function(){ 
    $(this).toggleClass("active").siblings().slideToggle('slow', function() {
        dashboard['pages'][page][$(this).closest('li').attr("id")]['show'] = $(this).is(":hidden") ? 'collapsed' : 'expanded';
    });
  }

我得到一个错误说dashboard.pages未定义

i get an error saying 'dashboard.pages is undefined'

有没有去动态地添加页,并随后没有,如果它被定义首先那么如果它不这样做不得不做检查,看看工作的孩子们

is there away to dynamically add 'pages' and the children that follow without having to do the work of checking to see if it is defined first then if its not doing

   dashboard['pages'] = {};

,因为有时他们可能已经存在,我不希望有检查树第一我只是想为需要建立分支机构

because sometimes they may already exist and I don't want to have to inspect the tree first i just want to build the branches as needed

修改
我改页面名称页面显示该页面的名称将发生变化,ALO我想指出的是,页面真的可以什么太。
这个想法是,你有一个可以包含参数ojbjecs不检查,看是否存在分支的任何对象

EDIT I changed 'pagename' to page to show that page names will change and alo I want to point out that the pages could really be anything too. the idea is that you have any object that can contain ojbjecs with parameters without checking to see if the branches exist

它看起来像$扩展为表示将去只是不知道该如何工作的方式。必须让我的周围的头

it looks like $extend as stated will be the way to go just not sure how that works. got to get my head around that

推荐答案

定义上的对象get和set方法。其实这可能只是对仪表对象,只有它的后代来定义,但是这是容易做到的。

Define get and set methods on an Object. Actually it could be defined just on the dashboard object and only its descendants, but that's easy to do.

Object.prototype.get = function(prop) {
    this[prop] = this[prop] || {};
    return this[prop];
};

Object.prototype.set = function(prop, value) {
    this[prop] = value;
}

通过嵌套的属性使用该的get()方法迭代,并调用设置()每当值必须集。

Iterate through nested properties using this get() method and call set() whenever a value has to be set.

var dashboard = {};

dashboard.get('pages').get('user').set('settings', 'oh crap');
// could also set settings directly without using set()
dashboard.get('pages').get('user').settings = 'oh crap';

console.log(dashboard); //​​​​​​​​​​​​​​​ {pages: {user: {settings: "oh crap"}}};

您还可以扩展/修改 GET 方法接受嵌套属性作为单独的参数的数组的的字符串。利用这一点,你只需要调用get一次:

You could also extend/modify the get method to accept the nested properties as individual arguments or an array or a string. Using that, you'd only have to call get once:

// get accepts multiple arguments here
dashboard.get('pages', 'user').set('settings', 'something');

// get accepts an array here
dashboard.get(['pages', 'user']).set('settings', 'something');

// no reason why get can't also accept dotted parameters
// note: you don't have to call set(), could directly add the property
dashboard.get('pages.user').settings = 'something';

更新

由于一般返回一个对象,不知道你是否需要一个数组或一些其他类型的对象,所以你将有get方法来指定自己:

Since the get method generically returns an object and does not know whether you need an array or some other type of object, so you would have to specify that yourselves:

dashboard.get('pages.user').settings = [];

然后,你可以推项目的设置阵列

Then you could push items to the settings array as

dashboard.get('pages.user').settings.push('something');
dashboard.get('pages.user').settings.push('something else');

要实际拥有get函数从给定的字符串构造对象的层次结构,如pages.user,你就必须将字符串分成几部分,并检查是否每个嵌套对象存在。这里是 GET 的修改版本,它做到了这一点:

To actually have the get function construct the object hierarchy from a given string such as pages.user, you would have to split the string into parts and check if each nested object exists. Here is a modified version of get that does just that:

Object.prototype.get = function(prop) {
    var parts = prop.split('.');
    var obj = this;
    for(var i = 0; i < parts.length; i++) {
        var p = parts[i];
        if(obj[p] === undefined) {
            obj[p] = {};
        }
        obj = obj[p];
    }
    return obj;
}

// example use
var user = dashboard.get('pages.user');
user.settings = [];
user.settings.push('something');
user.settings.push('else');

console.log(dashboard); // {pages: {user: {settings: ["something", "else"] }}}

// can also add to settings directly
dashboard.get('pages.user.settings').push('etc');

这篇关于添加到JSON属性,可能或可能不存在尚未的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆