按顺序将对象推入数组

时间:2019-04-29 12:31:32

标签: javascript typescript

在下面的JSON结构中,

[
  {
    "type": "heading-1",
    "text": "A title",
  },
  {
    "type": "ordered-list-item",
    "text": "Ordered Item A",
  },
  {
    "type": "unordered-list-item",
    "text": "Ordered Item B",
  },
  {
    "type": "heading-2",
    "text": "A title",
  },
  {
    "type": "ordered-list-item",
    "text": "Ordered Item A",
  },
  {
    "type": "unordered-list-item",
    "text": "Ordered Item B",
  }
];

我需要将ordered-list-itemunordered-list-item的所有类型移动到新对象中。像下面这样

  {
    "type": 'list',
    "items": [
      {
        "type": "ordered-list-item",
        "text": "Ordered Item A",
      },
      {
        "type": "unordered-list-item",
        "text": "Ordered Item B",
      }
    ]
  }

最重要的是,我需要维持订单

例如,应将ordered-list-itemunordered-list-item推入新对象,直到匹配type

因此,在具有上述Json结构的情况下,以下是预期的输出

[
  {
    "type": "heading-1",
    "text": "A title",
  },
  {
    "type": "heading-2",
    "text": "A title",
  },
  {
    "type": 'list',
    "items": [
      {
        "type": "ordered-list-item",
        "text": "Ordered Item A",
      },
      {
        "type": "unordered-list-item",
        "text": "Ordered Item B",
      }
    ]
  },
  {
    "type": "heading-1",
    "text": "A title",
  },
  {
    "type": 'list',
    "items": [
      {
        "type": "ordered-list-item",
        "text": "Ordered Item A",
      },
      {
        "type": "unordered-list-item",
        "text": "Ordered Item B",
      }
    ]
  },
]

这怎么办?

2 个答案:

答案 0 :(得分:1)

您可以在任何数组上使用array.filter来创建一个符合条件的新数组(顺序相同)

const orderedList = yourArray.filter(a => a.type === 'ordered-list-item');
const unOrderedList = yourArray.filter(a => a.type === 'unordered-list-item');

然后只需使用新的过滤后的数组构建新的json对象。

答案 1 :(得分:0)

function deconstruct(data) {
    let index = -1;
    const out = [];

    data.forEach(entry => {
        if (entry.type !== 'ordered-list-item' && entry.type !== 'unordered-list-item') {
            // If the current entry's type prop is no (un)ordered-list-item
            // We push it to the array and reset the index variable to -1
            out.push(entry);
            index = -1;
        } else {
            // Else, we check if index is -1. If it is, we push a new object
            // And save its index to the index variable
            if (index === -1) {
                index = out.push({ type: 'list', items: [] }) - 1;
            }

            // Add the entry to the items of the current list
            out[index].items.push(entry);
        }
    });

    return out;
}

这是另一种方法:

data.map((entry, index) => {
  return {...entry, index, use: entry.type !== 'unordered-list-item' && entry.type !== 'ordered-list-item'}
}).filter(entry => entry.use).map((entry, index, entries) => {
  const end = index < entries.length -1 ? entries[index + 1].index : data.length - entry.index;
  return [{type: entry.type, text: entry.text}, {type: 'list', items: data.slice(entry.index + 1, entry.index + end)}]
}).flat(2);