英文:
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 函数,将消息数组转换为以下形式:
[{
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...
// 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论