1

I have an array like this:

const data = [
  {"id": 3, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 4, "value": "a"},
  {"id": 8, "value": "d"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
  {"id": 5, "value": "z"},
  {"id": 8, "value": "h"},
  {"id": 8, "value": "b"},
]

and would like to delete all objects, where the value is not the same in one of the other objects. So, at the end I would like to have:

[
  {"id": 3, "value": "a"},
  {"id": 4, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 8, "value": "b"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
]

I tried several versions. How to check against something which you don´t know if it is there (later in the loop)? If I do check the list against itself in a loop, I am runing in the corner, that the array itself changes its length during the for loop... Is there a simple approach?

Thanks for any help!

Phil
  • 157,677
  • 23
  • 242
  • 245
Harald
  • 243
  • 2
  • 12
  • Does this answer of your question https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value? – Faheem azaz Bhanej Nov 17 '21 at 06:20
  • 1
    It is generally good to post the latest of your tries and why it didnt work, this shows the answerers that you put in the work required but are missing a few puzzle pieces – DownloadPizza Nov 17 '21 at 06:20
  • 2
    _"I tried several versions"_... so show them and then we can help you debug any issues – Phil Nov 17 '21 at 06:20
  • 1
    Oh also at id=2 value=d you are mising a colon and there are three d objects – DownloadPizza Nov 17 '21 at 06:21
  • Please post your attempts that you made. We are here to help when you are stuck with some issues, but we dont write entire code for you. – Nitheesh Nov 17 '21 at 06:25
  • Does this answer your question? [How to remove all duplicates from an array of objects?](https://stackoverflow.com/questions/2218999/how-to-remove-all-duplicates-from-an-array-of-objects) – MD. RAKIB HASAN Nov 17 '21 at 06:26
  • 2
    Your question is not clear. What exactly is the criteria? It almost looks like you want to eliminate the entries with unique `value` (like 5/z and 8/h) but what happened to 8/d? There are two other "d" values so it's not unique – Phil Nov 17 '21 at 06:27
  • You can try array.reduce method to check if the object is some in array or not ! – Sanmeet Nov 17 '21 at 06:52

3 Answers3

1

You need two passes in your array: the first one will count the number for each values, the second one will remove the objects with lone values:

function removeLoneValues(data) {
  const values = {}

  data.forEach(datum => {
    if (values[datum.value] === undefined) {
      values[datum.value] = 0
    }
    values[datum.value]++
  })

  data.filter(datum => {
    return values[datum.value] > 1
  })

  return data
}
SteeveDroz
  • 6,006
  • 6
  • 33
  • 65
0

You can achieve it like this:-

const data = [
  {"id": 3, "value": "a"},
  {"id": 3, "value": "b"},
  {"id": 4, "value": "a"},
  {"id": 8, "value": "d"},
  {"id": 1, "value": "d"},
  {"id": 2, "value": "d"},
  {"id": 5, "value": "z"},
  {"id": 8, "value": "h"},
  {"id": 8, "value": "b"},
]

let reqOutput = [];
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "a") {
        reqOutput.push(data[i]);
    }
}
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "b") {
        reqOutput.push(data[i]);
    }
}
for (let i = 0; i < data.length; i++) {
    if (data[i].value == "d") {
        reqOutput.push(data[i]);
    }
}

console.log(reqOutput);
-1

You can use Array.prototype.indexOf and Array.prototype.lastIndexOf alongside Array.prototype.filter and Array.prototype.map to achieve this:

const data = [
  {"id": 3, "value": 'a'},
  {"id": 3, "value": 'b'},
  {"id": 4, "value": 'a'},
  {"id": 8, "value": 'd'},
  {"id": 1, "value": 'd'},
  {"id": 2, "value": 'd'},
  {"id": 5, "value": 'z'},
  {"id": 8, "value": 'h'},
  {"id": 8, "value": 'b'},
];

const values = data.map((el) => el.value);

const nonUniqueData = data.filter((el) => values.indexOf(el.value) !== values.lastIndexOf(el.value));
console.log(nonUniqueData);

This code works by first creating a mapped array of only values, then by checking that the results of indexOf and lastIndexOf when finding that value in the mapped array are different we can check that the array is not unique. This heuristic is then used in a filter function to create a new version of your data array with all elements containing unique values removed.

If you need to sort your array based on its value, you can use Array.prototype.sort to do that based on whatever your values are. I've converted them to strings here so the code snippet will run.

Mark Hanna
  • 3,206
  • 1
  • 17
  • 25
  • The result is not matching with the result expected in the question. Its not my downvote. – Nitheesh Nov 17 '21 at 06:57
  • Yeah the result expected in the question doesn't seem to match the question's criteria. I'm assuming it's a mistake, but if not then the asker could clarify. – Mark Hanna Nov 17 '21 at 07:09
  • In this case its better to consider this question as invalid. You could vote the question to close instead of answering this unclear questions. The asker should validate whether the output is same as his requirements. We should value our efforts and time. – Nitheesh Nov 17 '21 at 07:19