0

How can I find items which have same name prop in the following array of objects with with ES6?

var pilots = [
  {
    id: 2,
    name: "Wedge Antilles",
    faction: "Rebels",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 40,
    name: "Iden Versio",
    faction: "Empire",
  },
  {
    id: 66,
    name: "Thane Kyrell",
    faction: "Rebels",
  }
];

var rebels = pilots.filter(function (item) {
  return item.name === item.name;
});


console.log(rebels)

I tried this solution but it didn't work, it's returned all items.

7
  • 2
    It returns all items because you're checking it against itself. Are you wanting to return all duplicates or just return ones that match a specific case? I.e. all that match name 'Ciena Ree'? Commented Sep 11, 2019 at 12:40
  • Your condition is always true, that's why it returns all the items. You need to change it by that, for example: return item.name === "Thane Kyrell"; Commented Sep 11, 2019 at 12:40
  • 1
    return item.name === item.name; <-- does the item's name matches the item name.... I hope it does Commented Sep 11, 2019 at 12:40
  • 2
    item.name === item.name ? comparing the value with itself will always return true you will get all the values in output Commented Sep 11, 2019 at 12:40
  • 1
    please add the wanted result as well. Commented Sep 11, 2019 at 12:41

3 Answers 3

3

the beauty of reduce - this method will eliminate the same objects

const tada = data.reduce((result, currentItem) =>
 result.includes(currentItem) ? result : [...result, currentItem], [])

we can specify what type of duplication we want to avoid (by property name)

const tada = data.reduce((result, currentItem) =>
 result.some(el => el.name === currentItem.name) ? result : [...result, currentItem], [])

if you want to find duplications (by name)

const tada = data.reduce(
  (result, currentItem, index) =>
    data.some(
      (el, innerIndex) => el.name === currentItem.name && index !== innerIndex
    )
      ? [...result, currentItem]
      : result,
  []
);
Sign up to request clarification or add additional context in comments.

Comments

2

The following code will return elements whose name appears in more than one element

var pilots = [
  {
    id: 2,
    name: "Wedge Antilles",
    faction: "Rebels",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 8,
    name: "Ciena Ree",
    faction: "Empire",
  },
  {
    id: 40,
    name: "Iden Versio",
    faction: "Empire",
  },
  {
    id: 66,
    name: "Thane Kyrell",
    faction: "Rebels",
  }
];

var rebels = pilots.filter(function (item, idx) {
  return pilots.some((v, i) => v.name === item.name && idx != i)
});

console.log(rebels)

Comments

0

Please use this code it will return which name are repeating in the pilots object

    <script type="text/javascript">
        var pilots = [
      {
        id: 2,
        name: "Wedge Antilles",
        faction: "Rebels",
      },
      {
        id: 8,
        name: "Ciena Ree",
        faction: "Empire",
      },
      {
        id: 8,
        name: "Ciena Ree",
        faction: "Empire",
      },
      {
        id: 40,
        name: "Iden Versio",
        faction: "Empire",
      },
      {
        id: 66,
        name: "Thane Kyrell",
        faction: "Rebels",
      }
    ];
    var obj = {};
    var rebels = pilots.filter(function (item) {
        if(!obj[item.name]) {
         obj[item.name] = item.name;
        } else {
            return item.name == item.name;
        }
    });
    console.log(rebels)
    </script>

Comments

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.