首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript:使用具有相同属性值的对象将对象数组转换为新数组,该属性值与子数组相同

Javascript:使用具有相同属性值的对象将对象数组转换为新数组,该属性值与子数组相同
EN

Stack Overflow用户
提问于 2020-06-12 17:42:57
回答 1查看 32关注 0票数 0

我正在使用Javascript将InDesign文件转换为ANF (Apple格式,即JSON)。

在ANF中,每个组件只能有一个段落样式(相当于文本帧)。

在InDesign中,文本帧中可以有更多的段落样式。作为解决办法,可以将多个组件放置在容器组件中,以保持布局正确。为此,当我在InDesign文档中找到一个具有多个段落样式的文本框架时,我创建一个容器组件,然后为每个段落创建一个组件,并将其嵌套在容器中。

如果文本框架中的两个或多个连续段落具有相同的段落样式,则需要将它们分组为一个嵌套组件。我知道这应该比较简单,但是在InDesign中工作的Javascript有点过时,所以没有很多现代的便利。我需要用它来创新。

下面是一个简化的数据示例:

代码语言:javascript
复制
var textContainer = {
  role: "container",
  components: [
    {
      textStyle: "style-1",
      text: "<p>0</p>",
    },
    {
      textStyle: "style-1",
      text: "<p>1</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>2</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>3</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>4</p>",
    },
    {
      textStyle: "style-1",
      text: "<p>5</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>6</p>",
    }
  ]
}

因此,在数组中,有两个位置,其中有两组组件,一个是连续的textStyle。我需要把这些放在一个组件里。所以当我完成时数据应该是这样的..。

代码语言:javascript
复制
var textContainer = {
  role: "container",
  components: [
    {
      textStyle: "style-1",
      text: "<p>0</p><p>1</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>2</p><p>3</p><p>4</p>",
    },
    {
      textStyle: "style-1",
      text: "<p>5</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>6</p>",
    }
  ]
}

本质上应该是[[0,1],[2,3,4],5,6]

我是这样开始的:

代码语言:javascript
复制
var simplifiedComponents = [];
var consecutive = ""
for(i=0;i<textContainer.components.length;i++){
    if(i > 0 && i < textContainer.components.length - 1){
        if(textContainer.components[i].textStyle == textContainer.components[i+1].textStyle){
            textContainer.components[i].text += textContainer.components[i+1].text;
            textContainer.components[i+1].text = "";
        }
    }
}

但我被卡住了。

有人知道我如何将我的数据转换成这个优化的版本吗?

再说一遍,我需要在这里使用基本的Javascript,因为我无法使用大多数的内容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 17:56:38

代码语言:javascript
复制
var textContainer = {
  role: "container",
  components: [
    {
      textStyle: "style-1",
      text: "<p>0</p>",
    },
    {
      textStyle: "style-1",
      text: "<p>1</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>2</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>3</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>4</p>",
    },
    {
      textStyle: "style-1",
      text: "<p>5</p>",
    },
    {
      textStyle: "style-2",
      text: "<p>6</p>",
    }
  ]
};

var simplifiedComponents = [];
var consecutive = ""
var components = textContainer.components;
for(var i = 0, l = components.length; i < l; i++){
  if (i !== 0 && components[i].textStyle !== components[i - 1].textStyle) {
    simplifiedComponents.push({text: consecutive, textStyle:components[i - 1].textStyle});
    consecutive = ""
  }
  consecutive = consecutive += components[i].text;
}
simplifiedComponents.push({text: consecutive, textStyle:components[components.length - 1].textStyle});


console.log(simplifiedComponents);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62349652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档