0

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)
          })
        }
      })
4
  • 5
    How did you attempt to solve this? Commented Aug 26, 2020 at 15:13
  • Array.isArray(list[key]) <= this check shouldn't be necessary. The result of a groupBy should always result in each key pointing to an array of elements, even if there is just one element. Commented Aug 26, 2020 at 15:15
  • It looks like your primary thing that is missing is that you are not checking to see if the names are different. You are just putting colors on things. Commented Aug 26, 2020 at 15:16
  • what is wrong with your output? Commented Aug 26, 2020 at 15:28

2 Answers 2

1

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/[email protected]/lodash.min.js"></script>

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

Comments

0

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/[email protected]/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

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.