首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JavaScript中组合对象数组中的continue值

在JavaScript中组合对象数组中的continue值
EN

Stack Overflow用户
提问于 2019-12-01 13:39:17
回答 1查看 37关注 0票数 0

我的结果应该合并连续的标签,如果标签是相同的,那么我想将这些文本组合成一个单独的文本。

因此,在下面的输入中,我的输出应该是:

代码语言:javascript
复制
Speaker-2:Do you want the systems in English or en espanol
Speaker-1:Heinous Spaniel
Speaker-2:For English press one for Spanish press 2 in Espanol
Speaker-1:Choirs a horror Janeiro at e ends in the pro gun to sober Tuesday to enter but I will have email but if you a new window
Speaker-2:but I put one that's 1 or 2.0 press he only those

代码语言:javascript
复制
   const note = [
    {
        text: 'do you want the systems in English or en espanol',
        tag: 2,
    },
    {
        text: 'heinous Spaniel',
        tag: 1,
    },
    {
        text: ' for English press one for Spanish press 2',
        tag: 2,
    },
    {
        text: ' in Espanol',
        tag: 2,
    },
    {
        text: ' choirs a horror Janeiro at e ends in the pro gun to sober Tuesday to enter',
        tag: 1,
    },
    {
        text: ' but I will have email but if you a new window',
        tag: 1,
    },
    {
        text: " but I put one that's 1 or 2.0 press he only those",
        tag: 2,
    },
];

const ip = note
    .map((e, i) => {
        const chanel = e.tag;
        let te = e.text.trim();
        const next = note[i - 1] ? note[i - 1].tag : 0;
        if (chanel === next) {
            te = `${note[i - 1].text.trim()} ${te}`;
            note[i - 1].text = te;
            note.splice(i, 1);
        }
        return {
            [`Speaker-${chanel}`]: te.charAt(0).toUpperCase() + te.slice(1),
        };
    })
    .filter(e => !!e);
let strimged = JSON.stringify(ip)
    .replace(/[{}"]/g, '')
    .split(',')
    .join('\n');
strimged = strimged.substring(1, strimged.length - 1);
console.log(strimged);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-01 14:20:09

Array.reduce()就是您的答案。

代码语言:javascript
复制
const note = [
    {
        text: 'do you want the systems in English or en espanol',
        tag: 2,
    },
    {
        text: 'heinous Spaniel',
        tag: 1,
    },
    {
        text: ' for English press one for Spanish press 2',
        tag: 2,
    },
    {
        text: ' in Espanol',
        tag: 2,
    },
    {
        text: ' choirs a horror Janeiro at e ends in the pro gun to sober Tuesday to enter',
        tag: 1,
    },
    {
        text: ' but I will have email but if you a new window',
        tag: 1,
    },
    {
        text: " but I put one that's 1 or 2.0 press he only those",
        tag: 2,
    },
];

 let previous = 0;

let result = note.reduce((total, item) => {

  if(item.tag === previous) {
    total += item.text;
  } else {
    total += `\nSpeaker-${item.tag}: ` + item.text;
    previous = item.tag;
  } 
  return total;
}, '');

console.log(result);

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

https://stackoverflow.com/questions/59122844

复制
相关文章

相似问题

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