循环遍历对象的对象

时间:2021-01-19 01:09:14

标签: javascript

所以我有以下功能香草JS代码:

function get_menu(menu_id) {
    wp.api.loadPromise.done(function() {
        const menus = wp.api.collections.Posts.extend({
            url: wpApiSettings.root + 'menus/v1/menus/' + menu_id,
        });
        const Menus = new menus();
        Menus.fetch().then(
            posts => {
            let post_list = posts.items;
            console.log(post_list);
        });
    });
}
get_menu(4);

这给了我一个对象的对象,如下所示: enter image description here

遍历这些对象并在其中呈现 HTML 的最佳方法是什么?因此,假设我想遍历每个对象并获取 post_title 并输出 HTML <div> + post_title + </div>

感谢所有帮助!

更新:

需要循环渲染:

<div class="column is-one-third is-flex py-0">
    <a href=" ***url*** " class="dropdown-item px-2 is-flex is-align-items-center">
        <figure class="image is-32x32 is-flex">
            <img src=" ***image*** + ***post_title*** '.svg'; ?>">
        </figure>
        <span class="pl-2"><?= ***post_title*** ?></span>
    </a>
</div>

1 个答案:

答案 0 :(得分:2)

您可以遍历数组并创建一个dom树

  var invalid = "";
  var rowWithError = "";
  data.forEach((row, i) => {
    if (row.join(",") == "Please Check Name, , , , , , , ,  ") {
      var wrongName = ss.getRange('A' + (i + 1)).getValue();
      invalid = wrongName;
      rowWithError = row.join(",");
    }
    if (invalid.length != 0){
      var ui = SpreadsheetApp.getUi();
      ui.alert(
      "The sheet had detected an invalid Name - '" + invalid + "', please check row " + rowWithError + " and try again.",)
    }
  });