menu

Questions & Answers

Passing Value to Next Foreach from Previous

I have 2 foreach populated from a JSON file.

First forEach(bookmark) I can get bookmark.title

Second forEach(parentId) I can't use bookmark.title coming from first one.

I want to use like 'title' = bookmark.title

Any ideas?


function create(nodeId, bookmarks, success) 
{
    let bookmarkList = new Set();
    bookmarks.forEach(bookmark => 
    {
        bookmarkList.add(bookmark.parent);
    });

    bookmarkList.forEach(parentId => 
    {
        if (parentId != 0)
        {
       
            chrome.bookmarks.create
            (
                {
                    'parentId': nodeId,
                    'title': parentId.title // this value returns empty]
                }, 
                function(newFolder)
                {
                    bookmarks.forEach(bookmark => 
                    {
                        if (bookmark.parent === parentId) 
                        {
                            chrome.bookmarks.create
                            (
                                {
                                    'parentId': newFolder.id,
                                    'title': bookmark.title,
                                    'url': bookmark.url
                                }
                            );
                        }
                    });
                }
            );
        }
    });
}

I tried many combinations.

Comments:
2023-01-20 23:10:02
can you share some data that you will pass to the function and the expected output
2023-01-20 23:10:02
data is already exist on first foreach loop as "bookmark.title"
2023-01-20 23:10:02
i want to use bookmark.title of first foreach(bookmark) in second foreach (parentId). i need to reach data like parentId.title which it will take from bookmark.title now parentId returns as ID but i can't reach title value of it.
Answers(0) :