Как найти различия во вложенных JSON объектах и ​​выделить их - PullRequest
0 голосов
/ 08 апреля 2020

У меня есть проблема, которую я не могу решить долгое время. У меня есть два вложенных объекта JSON, и мне нужно узнать, что является дополнениями во втором и выделить их (добавить класс CSS).

Например, если у меня было:

let obj1 = {
    blocks: [
      {
        type: 'paragraph',
        data: {
          text: 'This is text for my project.',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'This is in both and needs no change',
        },
      },
    ],
  }

  let obj2 = {
    blocks: [
      {
        type: 'paragraph',
        data: {
          text:
            'This is text for my project. This is some text that will not be in the first object.',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'Things are becoming complicated in programming',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'This is in both and needs no change',
        },
      },
    ],
  }

После их сравнения я бы хотел, чтобы второй объект был похож на:

let obj2 = {
    blocks: [
      {
        type: 'paragraph',
        data: {
          text:
            'This is text for my project. <span class="new-text">This is some text that will not be in the first object.</span>',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: '<span class="new-text">Things are becoming complicated in programming</span>',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'This is in both and needs no change',
        },
      },
    ],
  }

Я попытался просмотреть библиотеку diff из Google, но не могу найти другие вещи, которые могут быть чистыми строками или обычные объекты (не вложенные)

Любая помощь приветствуется.

Спасибо!

1 Ответ

0 голосов
/ 09 апреля 2020

Вы можете сделать что-то подобное, если я ошибаюсь, поправьте меня, я новичок.

   let obj1 = {
    blocks: [
      {
        type: 'paragraph',
        data: {
          text: 'This is text for my project.',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'This is in both and needs no change',
        },
      },
    ],
  }

  let obj2 = {
    blocks: [
      {
        type: 'paragraph',
        data: {
          text:
            'This is text for my project. This is some text that will not be in the first object.',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'Things are becoming complicated in programming',
        },
      },
      {
        type: 'paragraph',
        data: {
          text: 'This is in both and needs no change',
        },
      },
    ],
  }
obj2["blocks"].map(item => {
      var str = item.data.text;
      obj1["blocks"].map(item2 => {
        var obj2Str = item2.data.text;
        if (str.indexOf(obj2Str) > -1) {
          var endStr = str.substr(obj2Str.length);
          item.data.text = endStr.length >0 ? obj2Str + '<span class="new-text">'+ endStr +'</span>' : obj2Str;
        }
      });
    });

Надеюсь, это поможет:)

...