[英]find all duplicates on an array of objects using lodash
我有以下对象数组:
[
{ 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' }
]
如果它们具有相同的ip
但不相同的name
,我想为它们添加颜色,所以像这样。
[
{ 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' }
]
您如何使用 lodash 实现这一目标? 还是香草?
编辑:我试图编码它,但它产生不同的输出。
[
{ 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' }
]
这是我的代码。
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)
})
}
})
在设置颜色之前,您可以尝试使用every来检查 list[key] 的每个元素中的 name 和 ip 是否相同:
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>
一个问题可能是生成随机颜色:您可以两次使用颜色
固定代码:
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>
不使用 Lodash,在您的代码未添加到问题时编写。 悲伤,就删除。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.