简体   繁体   中英

find all duplicates on an array of objects using lodash

i have the following array of objects:

[
 { ip: 1, name: 'examplel' },
 { ip: 1, name: 'examplel' },
 { ip: 202.164.171.184, name: 'example2' },
 { ip: 202.164.171.184, name: 'example2' },
 { ip: 202.164.171.184, name: 'example3' },
 { ip: 127.0.0.1, name: 'example4' },
 { ip: 127.0.0.1, name: 'example5' }
]

and i want to put colors on them if they have the same ip but not equal name , so something like this.

[
 { ip: 1, name: 'examplel', color: '' },
 { ip: 1, name: 'examplel', color: '' },
 { ip: 202.164.171.184, name: 'example2', color: 'red' },
 { ip: 202.164.171.184, name: 'example2', color: 'red' },
 { ip: 202.164.171.184, name: 'example3', color: 'red' },
 { ip: 127.0.0.1, name: 'example4', color: 'black' },
 { ip: 127.0.0.1, name: 'example5', color: 'black' }
]

how can you achieve this using lodash? or vanilla?


edit: i tried to code it, but it producing different output.

[
 { ip: 1, name: 'examplel', color: 'red' },
 { ip: 1, name: 'examplel', color: 'red' },
 { ip: 202.164.171.184, name: 'example2', color: 'red' },
 { ip: 202.164.171.184, name: 'example2', color: 'red' },
 { ip: 202.164.171.184, name: 'example3', color: 'red' },
 { ip: 127.0.0.1, name: 'example4', color: 'black' },
 { ip: 127.0.0.1, name: 'example5', color: 'black' }
]

here's my code.

      let list = _.groupBy(data, 'ip')
      const colorList = ['pink', 'blue', 'pink', 'red']
      let logsList = []

      _.keys(list).forEach(key => {
        const color = colorList[Math.floor(Math.random() * colorList.length)]

        if (Array.isArray(list[key])) {
          list[key].forEach(data => {
            data.color = color
            logsList.push(data)
          })
        }
      })

You could try using every to check and see if the name and ip are the same in every element of list[key] before setting the color:

https://lodash.com/docs/4.17.15#every

 let data = [ { ip: '1', name: 'examplel' }, { ip: '1', name: 'examplel' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example3' }, { ip: '127.0.0.1', name: 'example4' }, { ip: '127.0.0.1', name: 'example5' } ]; let list = _.groupBy(data, 'ip') const colorList = ['pink', 'blue', 'pink', 'red'] let logsList = [] console.log('list: ' + JSON.stringify(list)); _.keys(list).forEach(key => { if (Array.isArray(list[key])) { let color = ''; if(!_.every(list[key], list[key][0])) { color = colorList[Math.floor(Math.random() * colorList.length)] } list[key].forEach(data => { data.color = color logsList.push(data) }) } }) console.log('logsList: ' + JSON.stringify(logsList));
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

Problems in your code

One problem can be in generating random colors: you can get on color twice

Fixed code:

 let data = [ // Example data { ip: 1, name: 'examplel' }, { ip: 1, name: 'examplel' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example3' }, { ip: '127.0.0.1', name: 'example4' }, { ip: '127.0.0.1', name: 'example5' } ]; let list = _.groupBy(data, 'ip') const colorList = ['pink', 'blue', 'red'] // Color should not be in this list twice! let logsList = [] _.keys(list).forEach(key => { if(!colorList.length) throw new Error('Not enough colors'); let color = colorList[Math.floor(Math.random() * colorList.length)] // Chose color colorList.splice(colorList.indexOf(color), 1); // Remove chosen color from the list // So, we cannot chos one color twice // Removed not-needed IF list[key].forEach(data => { data.color = color logsList.push(data) }) }) console.log(logsList)
 <!-- Get lodash --> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

Another working solution

Not using Lodash, written while your code was not added to question. Sad to delete.

 let datas = [ { ip: 1, name: 'examplel' }, { ip: 1, name: 'examplel' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example2' }, { ip: '202.164.171.184', name: 'example3' }, { ip: '127.0.0.1', name: 'example4' }, { ip: '127.0.0.1', name: 'example5' } ]; let colors = ['green', 'red', 'blue', 'purple', /* And some more... */]; // Colors for diffrent IP's let currentColor = 0, // Which color we are currently using - index in colors[] pastIp = null; // Past Ip let colored = datas.sort((a, b) => ('' + a.ip).localeCompare(b.ip)) // Sort IP's, // Same adresses will be grouped .map(i => { if(pastIp && i.ip != pastIp) currentColor++; // We have started colorig another IP (as example, past was 1.1.1.1, this is 2.2.2.2) // Else, we are looping same IP's (as example, past was 1.1.1.1, this is also 1.1.1.1) pastIp = i.ip; // Current will be past return {color: colors[currentColor], ...i}; // Add color property }) console.log(colored); // Do somethig with result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM