0

I have a js object array as below

[
[
 {file: 'file1',
  status: 'pending',
  time: '2021-08-13 11:20:00'
 },
 {file: 'file2',
  status: 'pending',
  time: '2021-08-13 11:20:00'
 }
],
[
 {file: 'file1',
  status: 'completed',
  time: '2021-08-13 09:30:00'
 },
 {file: 'file2',
  status: 'completed',
  time: '2021-08-13 09:30:00'
 }
],
[
 {file: 'file3',
  status: 'completed',
  time: '2021-08-14 06:50:00'
 }
]
]

And i need to filter the array based on the file field and remove the duplicated sub arrays. So the output should look like

[
[
 {file: 'file1',
  status: 'pending',
  time: '2021-08-13 11:20:00'
 },
 {file: 'file2',
  status: 'pending',
  time: '2021-08-13 11:20:00'
 }
],
[
 {file: 'file3',
  status: 'completed',
  time: '2021-08-14 06:50:00'
 }
]
]

What is the easiest way to do this. Can we use ES6 here?

2
  • 1
    "Can we use ES6 here" Well that's up to you. Otherwise, if I correctly guess your usecase, why not use an object with the file name (or hash to avoid duplicates) as keys ? Commented Aug 13, 2021 at 19:00
  • 1
    Can u provide an example code pls? Commented Aug 14, 2021 at 4:30

2 Answers 2

2

Try using Array.reduce.

Please see the code comments for logic.

const input = [
    [
        { file: 'file1', status: 'pending', time: '2021-08-13 11:20:00' },
        { file: 'file2', status: 'pending', time: '2021-08-13 11:20:00' }
    ],
    [
        { file: 'file1', status: 'completed', time: '2021-08-13 09:30:00' },
        { file: 'file2', status: 'completed', time: '2021-08-13 09:30:00' }
    ],
    [
        { file: 'file3', status: 'completed', time: '2021-08-14 06:50:00' }
    ]
]
const output = input.reduce((acc, curr) => {
    let isNodeExist = false;
    /*
     * Each node of the accumulator acc will be an array
     * Which means the accumulator acc is an array of arrays
     * The current node curr will also be an array
     * So we have to verify the array curr has the same set present
     * For that there should be a child node present in acc with the same length of curr
     * Also the each name of curr should be there in that matching node
     */
    acc.forEach((node) => {
        isNodeExist = node.length === curr.length;
        if (isNodeExist) {
            curr.forEach((currNode) => {
                isNodeExist = isNodeExist && (node.find((item) => item.file === currNode.file) !== undefined);
            })
        }
    });
    if(!isNodeExist) {
        acc.push(curr);
    }
    return acc;
}, []);
console.log(output);

Sign up to request clarification or add additional context in comments.

2 Comments

It seems there is one more condition to be considered. See comments to my answer for details.
@vsemozhebuty hmm. The logic for filtering is not correctly mentioned in the question. Any way I have updated my answer, just to make sure that not to post an incorrect solution.
1

If you need to save just the first instance with the same group of names, this is a possible way:

const data = [
  [
    {file: 'file1',
      status: 'pending',
      time: '2021-08-13 11:20:00'
    },
    {file: 'file2',
      status: 'pending',
      time: '2021-08-13 11:20:00'
    }
  ],
  [
    {file: 'file1',
      status: 'completed',
      time: '2021-08-13 09:30:00'
    },
    {file: 'file2',
      status: 'completed',
      time: '2021-08-13 09:30:00'
    }
  ],
  [
    {file: 'file3',
      status: 'completed',
      time: '2021-08-14 06:50:00'
    }
  ],
  [
    {file: 'file2',
      status: 'completed',
      time: '2021-08-14 06:50:00'
    }
  ],
];

const filteredData = [];
const seenFiles = new Set();

for (const group of data) {
  const fileNames = JSON.stringify(group.map(({ file }) => file).sort());

  if (!seenFiles.has(fileNames)) {
    seenFiles.add(fileNames);
    filteredData.push(group);
  }
}

console.log(filteredData);

6 Comments

Appreciate ur answer. Here, if you change last object name from file3 -> file2 , it's eliminating too. But I need to consider them as separate. If there are sub arrays within the parent array, and they are subset within themselves, they should be separate
@ahkam What do you mean by "subset within themselves"? It seems you have just 3 levels: main array — group subarrays — files.
group subarrays may have objects with same file. So i want to consider them as 2 different group subarrays
@ahkam Sorry, I still do not understand what distinguishes the object with file2 from the second subarray and the object with possible file2 from the third subarray. Why the former is considered duplicate and the latter is not. Maybe you need more explanation in the question...
Sorry if the question is not clear enough. If the sub arrays contain same number of objects with same name, it should be considered as duplicates. But if the size of the sub arrays are varying and one sub array is a subset of another, it should be considered as two different. Basically these sub arrays are the requests made by the user. If the sub array are equal in size and the file names are same, user has requested the file set twice. So i need to show it as a single request
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.