创建具有直接相同属性值的唯一数组列表

Create unique array list which has immediate same property values

提问人:T Inamdar 提问时间:11/11/2023 最后编辑:T Inamdar 更新时间:11/11/2023 访问量:56

问:

我有下面带有对象的数组,其中包括重复的类型值。我需要基于直接相同类型值的唯一对象数组。

注意: 如果任何对象具有紧邻的下一个相同类型值,则它将创建一个唯一的数组列表。否则,它将包含另一个具有相同类型值的唯一对象列表。

数组:

[ 
  { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, 
  { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, 
  { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, 
  { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, 
  { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } 
]

在上面的数组中,对象 2 紧跟在对象 1 之后,具有相同的类型 A 值,因此它与 A 组成一个唯一的列表。之后,对象 3,4,5 具有 B 型值的直接基础,因此它创建了另一个唯一列表。在对象 2 之后,对象 3 没有类似的“类型”值,因此第一个唯一列表到此结束。但同样,对象 5,6,7,8 具有直接基类型 A 值,因此它创建了另一个唯一列表。

我希望在输出下方(如果任何对象有一个接一个的相同值,那么它将创建唯一的列表,否则它将在那里结束并制作另一个单独的对象列表):

[
   {
      "type":"A",
      "values":[
         {
            "object":1,
            "start":"2018-07-08",
            "end":"2018-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":2,
            "start":"2018-12-31",
            "end":"2019-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   },
   {
      "type":"B",
      "values":[
         {
            "object":3,
            "start":"2019-06-26",
            "end":"2019-12-31",
            "type":"B",
            "user":"PQRS"
         },
         {
            "object":4,
            "start":"2019-12-31",
            "end":"2020-06-31",
            "type":"B",
            "user":"PQRS"
         }
      ]
   },
   {
      "type":"A",
      "values":[
         {
            "object":5,
            "start":"2021-12-31",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":6,
            "start":"2012-12-31",
            "end":"2022-06-26",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":7,
            "start":"2022-06-26",
            "end":"2022-12-31",
            "type":"A",
            "user":"ABCD"
         },
         {
            "object":8,
            "start":"2022-12-31",
            "end":"2023-06-26",
            "type":"A",
            "user":"ABCD"
         }
      ]
   }
]
JavaScript的 分组

评论

3赞 Kurt Hamilton 11/11/2023
如果可以,那将会有所帮助 1.提供更简单、更清晰的输入和输出示例, 2.格式化您的帖子以提高可读性, 3.显示您已经进行了哪些尝试。

答:

0赞 Marian Theisen 11/11/2023 #1

你想要的是类似 s 的东西,请参阅此处的文档 https://lodash.com/docs/4.17.15#uniqBylodashuniqBy

var input = [ { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" }, { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" }, { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" }, { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" }, { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" }, { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" }, { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" }, { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" } ]

var output = _.uniqBy(input, 'type')

// OR

var output = _.uniqBy(input, x => x.type)

// results in 
[
   {
      "object": 1,
      "start": "2018-07-08",
      "end": "2018-12-31",
      "type": "A",
      "user": "ABCD"
   },
   {
      "object": 3,
      "start": "2019-06-26",
      "end": "2019-12-31",
      "type": "B",
      "user": "PQRS"
   }
]

0赞 Jahedul Hoque 11/11/2023 #2

试试它是否适合你。

function createUniqueLists(inputArray) {
  const result = [];
  let currentList = [];

  for (let i = 0; i < inputArray.length; i++) {
    const currentObject = inputArray[i];
    const currentType = currentObject.type;

    if (i === 0 || currentType !== inputArray[i - 1].type) {
      // Start a new unique list for a different type
      if (currentList.length > 0) {
        result.push({ [currentList[0].type]: currentList });
        currentList = [];
      }
    }

    currentList.push(currentObject);

    // If the next object has a different type, create a new unique list
    if (i < inputArray.length - 1 && currentType !== inputArray[i + 1].type) {
      result.push({ [currentType]: currentList });
      currentList = [];
    }
  }

  return result;
}

const inputArray = [
  { "object": 1, "start": "2018-07-08", "end": "2018-12-31", "type": "A", "user": "ABCD" },
  { "object": 2, "start": "2018-12-31", "end": "2019-06-26", "type": "A", "user": "ABCD" },
  { "object": 3, "start": "2019-06-26", "end": "2019-12-31", "type": "B", "user": "PQRS" },
  { "object": 4, "start": "2019-12-31", "end": "2020-06-31", "type": "B", "user": "PQRS" },
  { "object": 5, "start": "2021-12-31", "end": "2022-12-31", "type": "A", "user": "ABCD" },
  { "object": 6, "start": "2012-12-31", "end": "2022-06-26", "type": "A", "user": "ABCD" },
  { "object": 7, "start": "2022-06-26", "end": "2022-12-31", "type": "A", "user": "ABCD" },
  { "object": 8, "start": "2022-12-31", "end": "2023-06-26", "type": "A", "user": "ABCD" }
];

const output = createUniqueLists(inputArray);
console.log(JSON.stringify(output, null, 2));

1赞 Nina Scholz 11/11/2023 #3

您可以对每个元素的类型进行闭包检查。如果相同,则添加到结果集的最后一个元素,如果不是,则创建一个新对象。更新。typetype

const
    data = [{ object: 1, start: "2018-07-08", end: "2018-12-31", type: "A", user: "ABCD" }, { object: 2, start: "2018-12-31", end: "2019-06-26", type: "A", user: "ABCD" }, { object: 3, start: "2019-06-26", end: "2019-12-31", type: "B", user: "PQRS" }, { object: 4, start: "2019-12-31", end: "2020-06-31", type: "B", user: "PQRS" }, { object: 5, start: "2021-12-31", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 6, start: "2012-12-31", end: "2022-06-26", type: "A", user: "ABCD" }, { object: 7, start: "2022-06-26", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 8, start: "2022-12-31", end: "2023-06-26", type: "A", user: "ABCD" }],
    result = data.reduce((type => (r, o) => {
        if (type === o.type) {
            r.at(-1)[type].push(o);
        } else {
            type = o.type;
            r.push({ [type]: [o] });
        }
        return r;
    })(), []);

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

不同的结果。

const
    data = [{ object: 1, start: "2018-07-08", end: "2018-12-31", type: "A", user: "ABCD" }, { object: 2, start: "2018-12-31", end: "2019-06-26", type: "A", user: "ABCD" }, { object: 3, start: "2019-06-26", end: "2019-12-31", type: "B", user: "PQRS" }, { object: 4, start: "2019-12-31", end: "2020-06-31", type: "B", user: "PQRS" }, { object: 5, start: "2021-12-31", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 6, start: "2012-12-31", end: "2022-06-26", type: "A", user: "ABCD" }, { object: 7, start: "2022-06-26", end: "2022-12-31", type: "A", user: "ABCD" }, { object: 8, start: "2022-12-31", end: "2023-06-26", type: "A", user: "ABCD" }],
    result = data.reduce((r, o) => {
        if (r.at(-1)?.type === o.type) r.at(-1).values.push(o);
        else r.push({ type: o.type, values: [o] });
        return r;
    }, []);

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