Polymer dom-repeat和Firestore - 将对象映射转换为数组

时间:2017-12-11 12:55:37

标签: arrays json object polymer google-cloud-firestore

Polymer dom-repeats仅接受数组数据类型。我使用Firestore时建议使用对象映射而不是数组,因为这些可以单独查询和更新,与数组元素不同。

问题是,如何转换此对象图:

{
  "subsections": {
    "0": {
      "title": "my title 1",
      "description": "my desc 1"
    },
    "1": {
      "title": "my title 2",
      "description": "my desc 2"
    }
  }
}

到这个数组:

{
"subsections": [
  {
    "title": "my title 1",
    "description": "my desc 1"
  },
  {
    "title": "my title 2",
    "description": "my desc 2"
  }
}

聚合物dom-repeat是这样的:

<template is="dom-repeat" items="{{_toArray(sections.subsections)}}">

通话:

_toArray(obj) {
  return Object.keys(obj).map(function (key) {
    return {
      name: key,
      value: obj[key]
    };
  });
}

问题是函数返回一个嵌套数组0和1,标题和描述嵌套。

帮助!我对所有这些前端开发都很陌生,所以任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

按如下所示更改_toArray功能

from  __future__ import print_function
import os
from pathlib import Path

#Specifying the file path and the working directory
relevant_file_path = os.fspath('c:/users\username\desktop\personal project')
   included_extensions = ('v15.02.','v16.01.','v17.01.','v13.01.')

#Create a csv file to store the directories
HOME = str(Path.home())
print(HOME)
file_path = os.path.join(HOME,'test3.csv')


#Get everything split
def files_directories(root_path):
   for dirpath, dirnames, filenames in os.walk(os.path.normcase(relevant_file_path)):
       for filename in filenames:
          for ext in included_extensions:
              if ext in filename.lower() and '.pdf' not in filename.lower():
                  with open(root_path,'a+') as f:
                       f.write(os.path.normcase(''.join((os.path.join(dirpath,filename),'\n'))))
                       f.close()

然后将方法调用为

_toArray(obj) {
  return Object.keys(obj).map(function (key) {
    return obj[key];
  });
}