1

我有这个对象包含对象数组('pages')

var obj = {
    "pages" : [
        {   
            "title": "title one",
            "sub" : [
                { "title" : "title one one"},
                { "title" : "title one two"}
            ]
        },
        { "title" : "title two" },
        { 
            "title" : "title three",
            "sub" : [
                { "title" : "title three one"},
                { "title" : "title three two"},
                { "title" : "title three three"}
            ]
        }
    ]
}

和这样的预期结果(将“slug”元素添加到“页面”中的每个对象)

{
    "pages" : [
        {
            "title" : "title one",
            "slug" : "title-one",
            "sub" : [ 
                {
                    "title" : "title one one",
                    "slug" : "title-one-one"
                },
                {
                    "title" : "title one two",
                    "slug" : "title-one-two"
                }
            ]
        },
        {
            "title" : "title two",
            "slug" : "title-two"
        },
        {
            "title" : "title three",
            "slug"  : "title-three",
            "sub" : [
                {
                    "title" : "title three one",
                    "slug" : "title-three-one"
                },
                {
                    "title" : "title three two",
                    "slug" : "title-three-two"
                },
                {
                    "title" : "title three three",
                    "slug" : "title-three-three"
                }
            ]
        }
    ]
}

我想我可以做到

function add_slug(array_of_obj)
{
    for (var item in array_of_obj) {

        //i am not sure about this:
        item.slug = item.title.replace(/ /g, '-');

        if(typeof item.sub !== undefined) {
            add_slug(item.sub);
        }
    }
}

add_slug(obj.pages);

但是我得到undefined item.title我一直在修改函数但它还不起作用所以我需要帮助。谢谢。

4

2 回答 2

2

item是一个字符串,表示对象的当前属性。您需要做的是使用item从对象中获取属性。

array_of_obj[item].slug = array_of_obj[item].title.replace(/ /g, '-');

if(array_of_obj[item].sub !== undefined) {
    add_item(array_of_obj[item].sub);
}

所以错误是由于item字符串没有title属性,所以它是undefined,使得调用.replace()不可能。


此外,您typeof在测试undefined. 最好只进行比较而不使用typeof.

此外,您的递归调用正在使用add_item而不是add_slug.


最后,在 JavaScript 中最好在forArray 上使用循环而不是for-in循环。这有几个原因。在您的具体情况下,它可能没有什么不同,但总体上它仍然是一种更好的做法。

所以你的最终功能看起来像这样:

function add_slug(array_of_obj)
{
    for (var i = 0; i < array_of_obj.length; i++) {

        array_of_obj[i].slug = array_of_obj[i].title.replace(/ /g, '-');

        if(array_of_obj[i].sub !== undefined) {
            add_slug(array_of_obj[i].sub);
        }
    }
}

 add_slug(obj.pages);

或者使用 ES5 特性,像这样:

function add_slug(array_of_obj)
{
    array_of_obj.forEach(function(item) {

        item.slug = item.title.replace(/ /g, '-');

        if(item.sub !== undefined) {
            add_slug(item.sub);
        }
    }); 
}

 add_slug(obj.pages);

或者另一种方法可能如下所示:

function add_slug(item) {

    item.slug = item.title.replace(/ /g, '-');

    if(item.sub !== undefined) {
        item.sub.forEach(add_slug);
    }
}

obj.pages.forEach(add_slug);

或这个:

obj.pages.forEach(function add_slug(item) {

    item.slug = item.title.replace(/ /g, '-');

    if(item.sub !== undefined) {
        item.sub.forEach(add_slug);
    }
});
于 2013-09-04T00:53:38.490 回答
2

更改为使用 for 循环而不是 for-in - 对数组更安全。

尝试这个:

function add_slug(array_of_obj)
{
    for(var pageIdx = 0; pageIdx < array_of_obj.length; ++pageIdx) {
        var curPage = array_of_obj[pageIdx];

        if(curPage.title) {
            curPage.slug = curPage.title.split(' ').join('-');
        }

        if(curPage.sub) {
            add_slug(curPage.sub);
        }
    }
}

结果:

{
  "pages": [
    {
      "title": "title one",
      "sub": [
        {
          "title": "title one one",
          "slug": "title-one-one"
        },
        {
          "title": "title one two",
          "slug": "title-one-two"
        }
      ],
      "slug": "title-one"
    },
    {
      "title": "title two",
      "slug": "title-two"
    },
    {
      "title": "title three",
      "sub": [
        {
          "title": "title three one",
          "slug": "title-three-one"
        },
        {
          "title": "title three two",
          "slug": "title-three-two"
        },
        {
          "title": "title three three",
          "slug": "title-three-three"
        }
      ],
      "slug": "title-three"
    }
  ]
} 
于 2013-09-04T01:01:36.320 回答