-1
  let all_map: [
    {
      "name": "sy map",
      "civilization": "sham",
      "continent": "Asia",
      "country": "damascuse",
      "price": 0.0
    },
    {
      "name": "sy map subscribe",
      "civilization": "sham",
      "continent": "Asia",
      "country": "damascuse",
      "price": 0.0
    }
  ]

How can I create an if statment to check if the country property has the same value as ("country": "damascuse") in the object to show only one of them?

disinfor
  • 10,865
  • 2
  • 33
  • 44
  • not sure if I understood correctly. From what I understood, what you want is, if the value from the key `country` is a duplicate. You want to show only one of them? – Chris G Feb 07 '23 at 20:28
  • @ChrisG I think that's correct. OP, what one do you want to keep if they are duplicates? – disinfor Feb 07 '23 at 20:30
  • yes i mean that + this api change by user so i dont know the index of objectes that have the duplicates values. – محمد جمال Feb 07 '23 at 21:14
  • Does this answer your question? [Get list of duplicate objects in an array of objects](https://stackoverflow.com/questions/53212020/get-list-of-duplicate-objects-in-an-array-of-objects) – pilchard Feb 07 '23 at 21:30
  • also [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) and [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) – pilchard Feb 07 '23 at 21:30
  • 1
    Does this answer your question? [Removing duplicate objects (based on multiple keys) from array](https://stackoverflow.com/questions/53542882/removing-duplicate-objects-based-on-multiple-keys-from-array) – markfila Feb 07 '23 at 21:38

2 Answers2

1

You haven't explained your desired result well, but if I'm understanding correctly, you want to filter out duplicate countries from this array. My thought is to use Array.reduce() to iterate through and create a new array:

const reducedMap = all_map.reduce((accumulator, current) => {
  if (accumulator.findIndex(x => x.country === current.country) === -1) {
    accumulator.push(current);
  }
  return accumulator;
}, []);

You didn't specify if you need to keep a specific one if there are duplicates. If that's the case, and, for example, you always want to keep the country that has "subscribe" in the name, then, when a duplicate country is reached in the reducer's iteration, you could add a regex conditional and splice the new object into the array in place of the old one, like so:

const reducedMap = all_map.reduce((accumulator, current) => {
  const countryIndex = accumulator.findIndex(x => x.country === current.country);
  if (countryIndex === -1) {
    accumulator.push(current);
  } else {
    if (!/subscribe/.test(accumulator[countryIndex].name)) {
      accumulator.splice(countryIndex, 1, current);
    }
  }
  return accumulator
}, []);
Cutler.Sheridan
  • 170
  • 2
  • 9
0

let all_map = [{
    "name": "sy map",
    "civilization": "sham",
    "continent": "Asia",
    "country": "damascuse",
    "price": 0.0
  },
  {
    "name": "sy map subscribe",
    "civilization": "sham",
    "continent": "Asia",
    "country": "damascuse",
    "price": 0.0
  }
]

if (all_map[0].country === all_map[1].country) {
  console.log(all_map[0])
}
Konrad
  • 21,590
  • 4
  • 28
  • 64