如果两个以下对象具有不同的值,如何修改对象数组?

发布于 2025-01-20 00:40:34 字数 1213 浏览 0 评论 0原文

考虑此Array

const arr = [
  {order: 1, mess:"test1"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 3, mess:"test3"},
  {order: 4, mess:"test4"},
]

ARR始终由order分类。

我想修改它以获取:

const arr = [
  {order: 1, mess:"test1"},

  {order: 2, mess:"test2", sep:true},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},

  {order: 3, mess:"test3", sep:true},

  {order: 4, mess:"test4", sep:true},
]

sep如果它具有orde> order不同的对象,则必须插入到对象中。

我无法编写此功能,即使它可能很简单(我想!)。
目前,我有这个,但是它根本不起作用,也不理解它的行为。

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]


arr.forEach(function(value, i) {
  i = Number(i)
  const y = i + 1
  if (arr[i] !== undefined && arr[y] !== undefined) {
    if (arr[i].order === arr[y].order) arr[y].sep = true;
  }
});

console.log(arr)

你有什么想法吗?谢谢

Consider this array:

const arr = [
  {order: 1, mess:"test1"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 3, mess:"test3"},
  {order: 4, mess:"test4"},
]

arr is always sorted by order.

I want to modify it to get:

const arr = [
  {order: 1, mess:"test1"},

  {order: 2, mess:"test2", sep:true},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},

  {order: 3, mess:"test3", sep:true},

  {order: 4, mess:"test4", sep:true},
]

sep must be inserted into an object if it has an order different from the preceding object.

I'm unable to write this function, even if it might be quite simple (I guess!).
At this time, I have this, but it doesn't work at all and I don't understand its behavior.

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]


arr.forEach(function(value, i) {
  i = Number(i)
  const y = i + 1
  if (arr[i] !== undefined && arr[y] !== undefined) {
    if (arr[i].order === arr[y].order) arr[y].sep = true;
  }
});

console.log(arr)

Do you have any idea? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独守阴晴ぅ圆缺 2025-01-27 00:40:34

您可以映射一个新数组并检查项目和前任。

const
    data = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }],
    result = data.map((o, i, a) => i && a[i - 1].order !== o.order
        ? { ...o, sep: true }
        : o
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could map a new array and check the item and the predecessor.

const
    data = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }],
    result = data.map((o, i, a) => i && a[i - 1].order !== o.order
        ? { ...o, sep: true }
        : o
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

缱绻入梦 2025-01-27 00:40:34

试试这个

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]

arr.forEach((obj,index)=>{
  if(index>0 && obj.order != arr[index-1]['order']){
    arr[index]['sep']=true
  }
});
console.log(arr)

Try this

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]

arr.forEach((obj,index)=>{
  if(index>0 && obj.order != arr[index-1]['order']){
    arr[index]['sep']=true
  }
});
console.log(arr)

浪漫人生路 2025-01-27 00:40:34

现有阵列的最简单更改

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];

arr.forEach((value, i) => {
  if (i && arr[i-1].order !== arr[i].order) arr[i].sep = true;
});
console.log(arr)

Simplest change of existing array

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];

arr.forEach((value, i) => {
  if (i && arr[i-1].order !== arr[i].order) arr[i].sep = true;
});
console.log(arr)

允世 2025-01-27 00:40:34

更改原始数组:

arr.forEach((el, idx) => {
  if (idx === 0) {
    el.sep = true;
  } else {
    if (el.order !== arr[idx-1].order) {
      el.sep = true;
    }
  }
})

Changes original array:

arr.forEach((el, idx) => {
  if (idx === 0) {
    el.sep = true;
  } else {
    if (el.order !== arr[idx-1].order) {
      el.sep = true;
    }
  }
})
标点 2025-01-27 00:40:34

以下是达到您想要的相同结果的另一种方法。

我相信一种更通用的方式。

arr.forEach((obj, index) => {
  
  if(JSON.stringify(obj[index+1]) === JSON.stringify(obj[index+2])) {
    obj[index].sep = true;
  }
})

Below is another way to achieve the same result as you want.

I believe a more generalized way.

arr.forEach((obj, index) => {
  
  if(JSON.stringify(obj[index+1]) === JSON.stringify(obj[index+2])) {
    obj[index].sep = true;
  }
})
青衫负雪 2025-01-27 00:40:34

continue

You can copy the array:

let arr2 = [...arr];

Shift it (so the first item is the second one!)

arr2.shift()

Loop over both

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];
let arr2 = [...arr];
arr2.shift();
const result = [arr[0]];
let sep = true;
for (const [v1, v2] of [arr, arr2]) if (v1.order===v2.order) result.push(v2); else result.push({...v2, sep})
console.log(result)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文