javascript - 在 JavaScript 中组合对象数组中的连续值

标签 javascript arrays json object

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

所以,从下面的输入我的输出应该是:

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

 

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

最佳答案

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

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

关于javascript - 在 JavaScript 中组合对象数组中的连续值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59122844/

相关文章:

javascript - THREE.js:远处光源转换的阴影

ios - 从多个 UITableView 选定单元格传递图像数组

java - 将字符串拆分为矩阵数组

json - Elixir/Phoenix 将 JSON 保存到 Postgres 数据库

java - 解析 Android Volley JSONArray 响应

javascript - 调用命名函数表达式

javascript - 如何像数据库一样浏览 JSON 产品?

javascript - 如何为随当前值滑动的 slider 制作标签?

java - 使用握手引理查找具有偶数和的子数组的数量

python - 使用 eval 将 json 转换为 dict 是一个不错的选择吗?