2

I have set of items in array. There are multiple objects are there in each item.

If we have found duplicated items based on particular object, have to add index with that key object

for Ex:

var values = [
    { name: 'someName' },
    { name: 'someName' },
    { name: 'some' },
    { name: 'some' },
    { name: 'single' }
]; 

It would come as

var values = [
        { name: 'someName' },
        { name: 'someName1' },
        { name: 'some' },
        { name: 'some1' },
        { name: 'single' }
    ];

How can i write a logic for this in pure javascript(No jQuery)

https://jsfiddle.net/MohaideenIsmail/kr88up0m/1/

3
  • Have you tried anything ? Commented Apr 18, 2016 at 7:22
  • I have added in jsfiddle but couldn't able to get logic Commented Apr 18, 2016 at 7:24
  • Added jsfiddle in a post Commented Apr 18, 2016 at 7:25

2 Answers 2

1

A solution with thisArgs of Array#forEach() as a temporary object for keeping the count.

var values = [{ name: 'someName' }, { name: 'someName' }, { name: 'some' }, { name: 'some' }, { name: 'single' }];

values.forEach(function (a) {
    if (!this[a.name]) {
        this[a.name] = { count: 0 };
        return;
    }
    a.name += ++this[a.name].count;
}, Object.create(null));

document.write('<pre>' + JSON.stringify(values, 0, 4) + '</pre>');

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

Comments

0

The question and the example are not very inline.

If we have found duplicated items based on particular object, have to add index with that key object

From the above sentence what i understand is, if we find a duplicate, either one of the followings shall be done. Since it's also unclear what happens if multiple duplicates exist, the code will handle all existing duplicates.

1. The first one's index value will be added to the duplicate object's name property value.

var values = [
              { name: 'someName' },
              { name: 'someName' },
              { name: 'some'     },
              { name: 'some'     },
              { name: 'single'   },
              { name: 'someName' },
              { name: 'some'     }
             ],
     redIx = values.reduce((p,c,i) => { !!p[c.name] ? p[c.name].push(i) : (p[c.name] = [], p[c.name].push(i))
                                        return p
                                      },{});console.log(redIx)
Object.keys(redIx).forEach( e => redIx[e].forEach((i,j) => j>0 && (values[i].name += redIx[e][0])));
document.write('<pre>' + JSON.stringify(values, 0, 2) + '</pre>');

2. The duplicate's own index value will be added to it's name property value.

var values = [
              { name: 'someName' },
              { name: 'someName' },
              { name: 'some'     },
              { name: 'some'     },
              { name: 'single'   },
              { name: 'someName' },
              { name: 'some'     }
             ],
      redIx = values.reduce((p,c,i) => { !!p[c.name] ? p[c.name].push(i) : p[c.name] = [];
                                         return p
                                       },{});
Object.keys(redIx).forEach( e => redIx[e].forEach(i => values[i].name += i));
document.write('<pre>' + JSON.stringify(values, 0, 2) + '</pre>');

3. "1" character will be added to the duplicate object's name property value like shown in the example.

var values = [
    	      { name: 'someName' },
              { name: 'someName' },
              { name: 'some'     },
              { name: 'some'     },
              { name: 'single'   },
              { name: 'someName' },
              { name: 'some'     }
             ],
     redIx = values.reduce((p,c,i) => { !!p[c.name] ? p[c.name].push(i) : p[c.name] = [];
                                        return p
                                      },{});
Object.keys(redIx).forEach( e => redIx[e].forEach(i => values[i].name += "1"));
document.write('<pre>' + JSON.stringify(values, 0, 2) + '</pre>');

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.