Javascript: remove property "isSeen" foreach message (object) except the last message (having all messages the "isSeen" property to false except last)

huangapple go评论68阅读模式
英文:

Javascript: remove property "isSeen" foreach message (object) except the last message (having all messages the "isSeen" property to false except last)

问题

// 创建一个 JavaScript 函数,用于移除消息数组中一系列消息对象的“isSeen”属性,除了最后一个消息对象...

我尝试了一切但仍然出现错误和问题...
这是我想要实现的 UI 图像...

Javascript: remove property "isSeen" foreach message (object) except the last message (having all messages the "isSeen" property to false except last)


// 目标:创建一个 JavaScript 函数,将消息数组转换为以下形式:

[{
      isSeen: false, // 之前:是真的...
      id: 1,
      content: "Hello",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: false, // 之前:是真的...
      id: 1,
      content: "Do you know someone who can help?",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: true, // <-
      id: 1,
      content:
        "Do you know someone who can help?",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: false,
      id: 1,
      content: "No BRO!",
      createdAt: new Date(),
      user: {
        name: "Ramy Hadid",
        isOwner: true,
      },
    },
    {
      isSeen: true,  // 在这里,设置为 true,因为这是由 Ramy 发送的最后一条消息...
      id: 1,
      content:
        "Stop asking me ridiculous questions, that's soo annoying, am starting to get annoyed of you, stop it!",
      createdAt: new Date(),
      user: {
        name: "Ramy Hadid",
        isOwner: true,
      },
    }]
英文:

// Make a javascript function that removes the "isSeen" property for every message object except the last one, of a sequence of messages in an array...

I've tried everything but kept getting bugs and errors...
Here is an image of the UI that i want to reach...

Javascript: remove property "isSeen" foreach message (object) except the last message (having all messages the "isSeen" property to false except last)


// GOAL: make a javascript function that turns the messages array like this:

[{
      isSeen: false, // before: it was true...
      id: 1,
      content: "Hello",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: false, // before: it was true...
      id: 1,
      content: "Do you know someone who can help?",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: true, // <-
      id: 1,
      content:
        "Do you know someone who can help?",
      createdAt: new Date(),
      user: {
        name: "John Doe",
        isOwner: false,
      },
    },
    {
      isSeen: false,
      id: 1,
      content: "No BRO!",
      createdAt: new Date(),
      user: {
        name: "Ramy Hadid",
        isOwner: true,
      },
    },
    {
      isSeen: true,  // Here, it's set to true because it's the last message sent by Ramy...
      id: 1,
      content:
        "Stop asking me ridiculous questions, that's soo annoying, am starting to get annoyed of you, stop it!",
      createdAt: new Date(),
      user: {
        name: "Ramy Hadid",
        isOwner: true,
      },
    }]

答案1

得分: 0

我找到了解决方案经过了很长时间

function splitArrayByOwner(arr) {
    const result = [];
    let tempArr = [];

    for (let i = 0; i < arr.length; i++) {
      if (tempArr.length === 0) {
        tempArr.push(arr[i]);
      } else if (tempArr[0].isOwner === arr[i].isOwner) {
        tempArr.push(arr[i]);
      } else {
        result.push(tempArr);
        tempArr = [arr[i]];
      }
    }

    if (tempArr.length > 0) {
      result.push(tempArr);
    }

    return result;
  }

  function joinArraysByOwner(arr) {
    const result = [];

    for (let i = 0; i < arr.length; i++) {
      result.push(...arr[i]);
    }

    return result;
  }

  messages = joinArraysByOwner(
    splitArrayByOwner(messages).map((sequence) => {
      sequence = sequence.map((message) => {
        message.isSeen = false;
        return message;
      });

      sequence[sequence.length - 1].isSeen = true;
      return sequence;
    })
  );
英文:

I found the solution, after a long time:

function splitArrayByOwner(arr) {
    const result = [];
    let tempArr = [];

    for (let i = 0; i < arr.length; i++) {
      if (tempArr.length === 0) {
        tempArr.push(arr[i]);
      } else if (tempArr[0].isOwner === arr[i].isOwner) {
        tempArr.push(arr[i]);
      } else {
        result.push(tempArr);
        tempArr = [arr[i]];
      }
    }

    if (tempArr.length > 0) {
      result.push(tempArr);
    }

    return result;
  }

  function joinArraysByOwner(arr) {
    const result = [];

    for (let i = 0; i < arr.length; i++) {
      result.push(...arr[i]);
    }

    return result;
  }

  messages = joinArraysByOwner(
    splitArrayByOwner(messages).map((sequence) => {
      sequence = sequence.map((message) => {
        message.isSeen = false;
        return message;
      });

      sequence[sequence.length - 1].isSeen = true;
      return sequence;
    })
  );

In case somebody wants it Javascript: remove property "isSeen" foreach message (object) except the last message (having all messages the "isSeen" property to false except last)

huangapple
  • 本文由 发表于 2023年4月20日 04:18:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定