Polymer dom-repeat and Firestore – convert object map to array

问题内容:

Polymer dom-repeats only accept array data types. I’m using Firestore it’s recommended to use object maps rather than array, as these can be queried and updated individually, unlike array elements.

Question is, how do I convert this object map:

{
  "subsections": {
    "0": {
      "title": "Computer Programmer",
      "description": "Computer blah blah"
    },
    "1": {
      "title": "Computer Programmer",
      "description": "Computer blah blah"
    }
  }
}

To this array:

{
"subsections": [
  {
    "title": "Liverpool",
    "description": "Liverpool blah blah"
  },
  {
    "title": "Teesside",
    "description": "Teesside blah blah"
  }
}

Polymer dom-repeat is this:

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

Calling:

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

Problem is function returns a nested array of 0 and 1 with title and description nested.

Help! I’m new to all this front end development so any help greatly appreciated.

问题评论:

答案:

答案1:

Change your _toArray function as follows

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

Then call the method as

obj.subsections = _toArray(obj.subsections);

答案评论:

    
Bingo! Thank you for taking the time to help.
– Halloween Man
32 mins ago

原文地址:

https://stackoverflow.com/questions/47753486/polymer-dom-repeat-and-firestore-convert-object-map-to-array

添加评论

友情链接:蝴蝶教程