簡體   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

我正在使用 Javascript 將 InDesign 文件轉換為 ANF(Apple 新聞格式,即 JSON)。

在 ANF 中,每個組件只能有一個段落樣式(相當於文本框架)。

在 InDesign 中,文本框架中可以有多個段落樣式。 作為一種解決方法,可以將多個組件放置在容器組件內以保持布局正確。 為此,當我在 InDesign 文檔中發現一個文本框有多個段落 styles 時,我創建了一個容器組件,然后為每個段落創建一個組件並將其嵌套在容器內。

如果文本框架中的兩個或多個連續段落具有相同的段落樣式,我需要將它們組合成一個嵌套組件。 我知道這應該相對簡單,但是在 InDesign 中工作的 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的組件。 我需要將它們放入一個組件中。 所以當我完成后數據應該是這樣的......

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]

我是這樣開始的:

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,因為我無法使用大多數 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM