LaVOZs

The World’s Largest Online Community for Developers

'; javascript - How to find matching items in array of objects with ES6 - LavOzs.Com

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.

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,
  []
);

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)

You can try this.

function search(nameKey, pilots){
    for (var i=0; i < pilots.length; i++) {
        if (pilots[i].name === nameKey) {
            return pilots[i];
        }
    }
}

var array = [
    { id:1, name:"Wedge Antilles", faction: "Rebels" },
    { id:2, name:"Wedge Antilles", faction: "Rebels" }
];

var resultObject = search("1", array);

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>