skip to Main Content

How to set the duplicate value is an array object to empty string.

const arr= [
{ name: 'someName1', age: 12 },
{ name: 'someName2', age: 13 },
{ name: 'someName3', age: 14 },
{ name: 'someName4', age: 15 }, 
{ name: 'someName4', age: 16 }, 
{ name: 'someName5', age: 15 },
{ name: 'someName3', age: 17 }, 
{ name: 'someName2', age: 18 },
];

expected result like below:

const resultArr = [
  { name: 'someName1', age: 12 },
  { name: 'someName2', age: 13 },
  { name: 'someName3', age: 14 },
  { name: 'someName4', age: 15 }, 
  { name: '', age: 16 },
  { name: 'someName5', age: 15 },  
  { name: '', age: 17 }, 
  { name: '', age: 18 },
]

I have tried but not getting exact result,

arr.map((res, i=index) => {
      const getNum = i === 0 ? i : i - 1;
      const prev = arr[getNum].name;

      if (res.name === prev) {
          res.name = '';
      }

3

Answers


  1. Your function is incorrect. It only compares the current value with the previous one, and always clears the name at index 0.

    A more effective approach would be to initialize an array of strings before the map function, and update this array whenever a new name is encountered. Then, simply check if the current name already exists in the array.

    const existingNames = [];
    arr.map((res) => {
      if(existingNames.includes(res.name)) {
        res.name = '';
      } else {
        existingNames.push(res.name)
      }
      return res;
    });
    
    Login or Signup to reply.
  2. You could take a Set in a closure for seen names and map either the object or take a new object with empty name property.

    const 
        data = [{ name: 'someName1', age: 12 }, { name: 'someName2', age: 13 }, { name: 'someName3', age: 14 }, { name: 'someName4', age: 15 },  { name: 'someName4', age: 16 },  { name: 'someName5', age: 15 }, { name: 'someName3', age: 17 },  { name: 'someName2', age: 18 }],
        result = data.map((seen => o => seen.has(o.name) || !seen.add(o.name)
            ? { ...o, name: '' }
            : o
        )(new Set));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  3. You can keep track of the names you’ve seen with a Set while you’re mapping over the data to create the new data.

    const arr = [
      { name: "someName1", age: 12 },
      { name: "someName2", age: 13 },
      { name: "someName3", age: 14 },
      { name: "someName4", age: 15 },
      { name: "someName4", age: 16 },
      { name: "someName5", age: 15 },
      { name: "someName3", age: 17 },
      { name: "someName2", age: 18 },
    ];
    
    const seenNames = new Set();
    
    const newArr = arr.map((el) => {
      if(seenNames.has(el.name)) {
        // If we've seen the name already, replace it with an empty string.
        return {...el, name: ''};
      }
      // Record the name, and return the element as-is.
      seenNames.add(el.name);
      return el;
    });
    
    console.log(newArr);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search