简体   繁体   English

Javascript:将对象数组转换为新数组,其中具有相同属性值的对象连续放入子数组

[英]Javascript: convert array of objects to new array with objects that have a property value that is the same consecutively put into subarrays

I am converting InDesign files into ANF (Apple News Format, which is JSON) using Javascript.我正在使用 Javascript 将 InDesign 文件转换为 ANF(Apple 新闻格式,即 JSON)。

In ANF you can only have one paragraph style per component (the equivalent of a text frame).在 ANF 中,每个组件只能有一个段落样式(相当于文本框架)。

In InDesign there can be more that one paragraph style in a text frame.在 InDesign 中,文本框架中可以有多个段落样式。 As a workaround, multiple components can be placed inside a container component to keep your layout correct.作为一种解决方法,可以将多个组件放置在容器组件内以保持布局正确。 To this end when I find a text frame in the InDesign document with multiple paragraph styles, I create a container component and then create a component for each paragraph and nest it inside the container.为此,当我在 InDesign 文档中发现一个文本框有多个段落 styles 时,我创建了一个容器组件,然后为每个段落创建一个组件并将其嵌套在容器内。

In the case where two or more CONSECUTIVE paragraphs in the text frame have the same paragraph style, I need to group them into one nested component.如果文本框架中的两个或多个连续段落具有相同的段落样式,我需要将它们组合成一个嵌套组件。 I know this should be relatively simple, but the Javascript that works inside InDesign is a bit archaic, so a lot of the modern conveniences are not available.我知道这应该相对简单,但是在 InDesign 中工作的 Javascript 有点陈旧,所以很多现代便利设施都不可用。 I am needing to get creative with it.我需要发挥创意。

Here's a simplified example of the data:这是数据的简化示例:

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>",
    }
  ]
}

So in the array there are two places where there are two groups of components same textStyle consecutively.所以在数组中有两个地方连续有两组相同textStyle的组件。 I need to put these into one component.我需要将它们放入一个组件中。 So the data should look like this when I'm done...所以当我完成后数据应该是这样的......

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>",
    }
  ]
}

essentially it should be [[0,1],[2,3,4],5,6] ;本质上应该是[[0,1],[2,3,4],5,6]

I've started like this:我是这样开始的:

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 = "";
        }
    }
}

But I got stuck.但我被卡住了。

Anyone have an idea of how I can convert my data to this optimized version?任何人都知道如何将我的数据转换为这个优化版本?

Again, I need to use basic Javascript here as most of the ES5 stuff isn't available to me.同样,我需要在这里使用基本的 Javascript,因为我无法使用大多数 ES5 的东西。

 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过选定的属性将对象数组转换为对象的子数组 (javascript) - Convert array of objects into subarrays of objects by a selected property (javascript) 计算具有特定属性的相同值的元素,并将结果放入对象数组中 - Count elements that have the same value for a specific property and put the result in an array of objects 查找具有相同属性值的对象并将它们添加到新的数组属性中 - Find objects with same property value and add them in new array property 在javascript中的对象数组中从具有空值的数组创建一个新属性 - Create a new property from an array with null value in an array of objects in javascript 将值数组转换为具有该值作为属性的对象数组 - Convert array of values into array of objects with that value as a property 使用与值相同的键将数组转换为对象数组 - Convert array to array of objects with key as same as value JavaScript:从对象数组中的对象数组中提取属性值作为数组 - JavaScript: from an array of objects of an array of objects, extract value of a property as array 将具有默认值的新属性添加到 javascript 对象数组 - Add new property with default value to javascript array of objects 如何在 javascript 中检索具有相同属性值的对象数组 - How to retrieve the array of objects having same property value in javascript 如何获取具有相同属性的对象并在数组中具有最大值 - Javascript - how to get objects with the same property and has the max value in array - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM