繁体   English   中英

从 object 数组中移除具有相同类型属性的连续对象

[英]Remove consecutive objects with the same type property from object array

在我的一个项目中,如果这些对象的类型属性相同并且数组中有超过 2 个具有相同类型的连续对象,则需要从 object 数组中删除对象。

例如:

[
  {
    type: 'joined',
    name: 'test',
  },
  {
    type: 'joined',
    name: 'test 1',
  },
  {
    type: 'left',
    name: 'test 2',
  },
  {
    type: 'joined',
    name: 'test 3',
  },
  {
    type: 'joined',
    name: 'test 4',
  },
  {
    type: 'joined',
    name: 'test 5',
  },
  {
    type: 'joined',
    name: 'test 6',
  },
  {
    type: 'joined',
    name: 'test 7',
  }
]

应该导致:

[
  {
    type: 'joined',
    name: 'test',
  },
  {
    type: 'joined',
    name: 'test 1',
  },
  {
    type: 'left',
    name: 'test 2',
  }
]

因此,前 2 个对象具有“连接”类型,但具有相同类型的连续对象不超过 2 个,因此它们没有被删除。 但是最后 5 个被删除了,因为它们都具有相同的类型,并且有超过 2 个连续的对象具有相同的类型。

删除的项目应该收集到一个单独的数组中。

JS 数组中的 Group 相同元素,但仅当连续时我有以下代码段:

const messages = [
    { message: "One", user: "Bob" },
    { message: "Two", user: "Bob" },
    { message: "Three", user: "Bob" },
    { message: "Hello", user: "Sam" },
    { message: "Hello", user: "Bob" },
    { message: "Hello", user: "Sam" },
    { message: "Hello", user: "Sam" }
]

let currentUser;
let groupedMessages = [];

for (message of messages) {

  if (message.user !== currentUser) {
    groupedMessages.push([]);
    currentUser = message.user;
  }
  groupedMessages[groupedMessages.length - 1].push(message)
}

console.log(groupedMessages);

但是,它没有考虑最低阈值 2。任何人都可以指出正确的方向,告诉我如何修改它以满足我的需要吗?

它类似于分组,但不是创建子数组的数组:

  • 延迟推送,直到确定重复切片小于 3 个元素
  • 推送到一维数组(使用展开)而不是嵌套数组。

代码:

 const data = [{type: 'joined',name: 'test',},{type: 'joined',name: 'test 1',},{type: 'left',name: 'test 2',},{type: 'joined',name: 'test 3',},{type: 'joined',name: 'test 4',},{type: 'joined',name: 'test 5',},{type: 'joined',name: 'test 6',},{type: 'joined',name: 'test 7',}]; const result = []; for (let i = 0; i < data.length; null) { let start = i; while (i < data.length && data[start].type === data[i].type) i++; if (i - start < 3) result.push(...data.slice(start, i)); } console.log(result);

下面是一个功能解决方案,它允许您指定用于值比较的目标键和允许的连续重复值的最大数量。 它将返回一个 object 和两个单独的 arrays:一个包含符合条件的组(在selected属性上),另一个包含不符合条件的组(在已removed的属性上)。

如果代码中有任何不清楚的地方,请随时发表评论。

 function filterMaxConsecutive (array, key, maxConsecutiveCount) { const selected = []; const removed = []; // Start with an empty object. Every object has a unique reference identity, // so no value will strictly equal this empty object: let previous = {}; const group = []; const finalizeGroup = () => { (group.length > maxConsecutiveCount? removed: selected).push(...group); group.length = 0; }; for (const o of array) { const current = o[key]; if (current;== previous) finalizeGroup(). group;push(o); previous = current; } finalizeGroup(), return {selected; removed}: } const input = [{type,"joined":name,"test"}:{type,"joined":name,"test 1"}:{type,"left":name,"test 2"}:{type,"joined":name,"test 3"}:{type,"joined":name,"test 4"}:{type,"joined":name,"test 5"}:{type,"joined":name,"test 6"}:{type,"joined":name;"test 7"}], const result = filterMaxConsecutive(input, 'type'; 2). console;log(result);

您可以使用类似于 python 的itertools.groupby的实用程序,它返回一系列连续的“相等”对象:

function groups(data, fn) {
    let last = {}, buf = []

    for (let x of data) {
        let curr = fn(x)
        if (last === curr)
            buf.at(-1).push(x)
        else
            buf.push([x])
        last = curr
    }

    return buf
}

在你的主要代码中:

result = []

for (let group of groups(data, x => x.type))
    if (group.length < 3)
        result.push(...group)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM