1
var indianTeam = [{
      firstName: "KL",
      lastName: "Rahul"
    }, {
      firstName: "Jayant",
      lastName: "Yadav"
    }, {
      firstName: "Umesh",
      lastName: "Yadav"
    }];

In above array, you can find lastname in 2nd and 3rd have duplicate values "yadav" so I want to find second duplicate (i.e umesh yadav) and replace it with its first name, leaving jayant yadav as unique.

Function I used so far

services.filterPlayers = function() {

  var i,j,tempArray = [];  

  for (i = 0; i < indianTeam.length; i++) {
      tempArray.push(indianTeam[i].lastName);
  }

  tempArray = tempArray.filter(function(elem, pos){
    if (tempArray.indexOf(elem) !== pos) {
      return true;
    }     
  });

  return tempArray;
};

Scenario one: I filtered out duplicate yadav and returned unique values to temporary with == condition signs,

Scenario two: only yadav returned to temporary with !== condition

How to replace duplicate yadav with firstname and push to same position in temporary array?

Reference : codepen link

4
  • replace? filter? unique? what do you want? Commented Feb 8, 2017 at 15:42
  • i want array like ['rahul','yadav','umesh'] Commented Feb 8, 2017 at 15:47
  • @Arun oh. now ive got you. updated my answer... Commented Feb 8, 2017 at 15:52
  • @Arun, please add the information to the question. Commented Feb 8, 2017 at 15:55

2 Answers 2

1

You could use a hash table for the lastNameand check if set. If set, then take firstName, otherwise lastName.

var indianTeam = [{ firstName: "KL", lastName: "Rahul" }, { firstName: "Jayant", lastName: "Yadav" }, { firstName: "Umesh", lastName: "Yadav" }, { firstName: "jane" }, { firstName: "joe" }],
    hash = Object.create(null),
    result = indianTeam.map(function (a) {
        if (!a.lastName || hash[a.lastName]) {
            return a.firstName;
        }          
        hash[a.lastName] = true;
        return a.lastName;
    });

console.log(result);

Sign up to request clarification or add additional context in comments.

7 Comments

do you have undefined values?
when i ran in my full code , only first position has last name and all other has first name .From ur snippet , it should display as Rahul,Yadav,Umesh ... Only umesh has to first name not others.
Also i dont know how hash object compares with result array for duplicates?
do you need to check for duplicates in the result array?
yes when code start finding duplicates , it should replace with its first name. Sorry for not stating the goal clearly i think
|
1

This gives you a unique lastName array:

new Set(indianTeam.map(el=>el.lastName));

This creates a unique lastName, fallback firstName array:

indianTeam=indianTeam.map((el,i)=>indianTeam.findIndex(e=>e.lastName==el.lastName)===i?el.lastName:el.firstName);

http://jsbin.com/sozeficafa/edit?console

You could use the map function wich replaces each element by the returned one. The code checks if the Element is the first found, if so it keeps it, if not it replaces it by the first.

6 Comments

Thanks Jonas w for answer but it didnt satisy my result as run ur code in jsbin
@arun it equals the example youve given. by the way dont trust the jsbin sth has been corrupted there...
oh k Jonas but its difficult understand a bit .. Es6 all involved :D
Jonas it works but same as Nina it replaces all values with first name after array first value(with lastname)
@Arun could you give a non working case, input -> output ?
|

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.