-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?

6
  • 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? Commented Feb 7, 2023 at 20:28
  • @ChrisG I think that's correct. OP, what one do you want to keep if they are duplicates? Commented Feb 7, 2023 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. Commented Feb 7, 2023 at 21:14
  • Does this answer your question? Get list of duplicate objects in an array of objects Commented Feb 7, 2023 at 21:30
  • 1
    Does this answer your question? Removing duplicate objects (based on multiple keys) from array Commented Feb 7, 2023 at 21:38

2 Answers 2

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
}, []);
Sign up to request clarification or add additional context in comments.

2 Comments

yes of course and thank you again (If possible I can take your number or any way to communication ?)
Missed this when you commented. I won't post my number on here but if you have any questions about this solution feel free to ask.
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])
}

2 Comments

in fact this is api has more other maps and the user has ability to add to it so i cant know the index of objects that has the same values
i will thankful if you know how i do this if i dont know the index

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.