开发者

searching and inserting javascript object

开发者 https://www.devze.com 2023-04-13 01:54 出处:网络
I\'ve been working on a layout library. it is broken up into panels object. I need to search the nested elements for an id and insert data before or after the target element. I can use splice to place

I've been working on a layout library. it is broken up into panels object. I need to search the nested elements for an id and insert data before or after the target element. I can use splice to place the new element but what I can't work out how is search the obj. If posible I would prefer not to use an external library.

any advice or help with this would be very much appreciated

here is an example of a panel object

panel['menu'] = 
{
"id":"menu",
"css":"panel",
"floating":true,
"elements":[{
           "id":"menu-header",
           "html":"<h1 class=\"header\">Loading</h开发者_StackOverflow社区1>",
           "index":0,
           "sib_count":1
           }]
};


You can just iterate through the array and look for the id you want

var elements = panel['menu']['elements'];
for ( var i = 0; i < elements.length; i++ ) {
    if ( elements[i]['id'] === "some id" ) {
        elements.splice( i, 0, /* new elements */ );
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消