映射回更新的JSON值

时间:2018-09-11 12:12:54

标签: arrays angularjs json mapping

之前,我尝试从JSON return中提取信息,在这里我需要从数组中提取键值:-

$scope.jsonObj = {
  "stylesheet": {
    "attribute-set": [
      {
        "attribute": {
          "_name": "text-align",
          "__prefix": "xsl",
          "__text": "center"
        },
        "_name": "__frontmatter",
        "__prefix": "xsl"
      },
      {
        "attribute": [
          {
            "_name": "space-before",
            "__prefix": "xsl",
            "__text": "80mm"
          },
          {
            "_name": "line-height",
            "__prefix": "xsl",
            "__text": "140%"
          }
        ],
        "_name": "__frontmatter__title",
        "_use-attribute-sets": "common.title",
        "__prefix": "xsl"
      }
    ],
    "_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
    "_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
    "_version": "2.0",
    "__prefix": "xsl"
  }
};

$scope.textvalue =$scope.jsonObj.stylesheet['attribute-set']
    .map(x => {
        if (Array.isArray(x.attribute))
            return x.attribute.map(y => y['__text']);
        else
            return [x.attribute['__text']]; 
    })
    .reduce((accu, cur) => accu.concat(...cur), []);

console.log($scope.textvalue);

由此我得到一个数组:-$scope.textvalue=["center", "80mm","140%"]

因此,现在我从视图中更新数组,并在单击同一对象$scope.textvalue时保存相同的值。

我需要将新的textvalue更新回JSON对象$scope.jsonObj

如何将更新后的数组映射回与__text对象相同的jsonObj值?

0 个答案:

没有答案