JavaScript Combine array of objects, combine key with same value and keep unique value combine array objects javascript,concat array objects javascript,concat array

This is my data with 5 arrays. What I wish to achieve is to combine id and name and the new array should have 5 different playname values. It can be in either an array or new key like playername1.

[
  {
    "id": 1,
    "name": "Liquid",
    "playername": "GH",
  },
  {
    "id": 1,
    "name": "Liquid",
    "playername": "KuroKy",
  },
  {
    "id": 1,
    "name": "Liquid",
    "playername": "Miracle",
  },
  {
    "id": 1,
    "name": "Liquid",
    "playername": "w33",
  },
  {
    "id": 1,
    "name": "Liquid",
    "playername": "Mind-Control",
  }

]

I am using lodash to try and achieve this but I am not able to get the data format I want using the code examples I have searched online.

This is my current code that I have tried that gives an array that is grouped by the ID.

    _.forOwn(this.state.teamsData, function(value, key) {
      console.log(value);
    });

The original data are not grouped by ID.

I am trying to get my data to look like this {"id": 1, "name": liquid, "playername": "GH", "playername2": "KuroKy" ....}

Answer:1

You could group by id and name properity and store the index for the same group.

var data = [{ id: 1, name: "Liquid", playername: "GH" }, { id: 1, name: "Liquid", playername: "KuroKy" }, { id: 1, name: "Liquid", playername: "Miracle" }, { id: 1, name: "Liquid", playername: "w33" }, { id: 1, name: "Liquid", playername: "Mind-Control" }],
    result = Object
        .values(data.reduce((r, { id, name, playername }) => {
            var key = [id, name].join('|');
            r[key] = r[key] || { data: { id, name }, index: 0 };
            r[key].data['playername' + (r[key].index++ || '')] = playername;
            return r;
        }, {}))
        .map(({ data }) => data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:2

Group by a combination of id and name (${o.id}~~~${o.name}). Map the groups, extract the name and id from the 1st item, take the player names, and use _.mapKeys() to convert the indexes to object keys. Combine the id, name, and playername properties to a single object using spread.

const teamsData = [{"id":1,"name":"Liquid","playername":"GH"},{"id":1,"name":"Liquid","playername":"KuroKy"},{"id":1,"name":"Liquid","playername":"Miracle"},{"id":1,"name":"Liquid","playername":"w33"},{"id":1,"name":"Liquid","playername":"Mind-Control"}]

const result = _(teamsData)
  .groupBy(o => `${o.id}~~~${o.name}`) // group by id and name
  .map(group => ({ // map the groups
    ..._.pick(_.head(group), ['id', 'name']), // take id and name from 1st item
    ..._.mapKeys(_.map(group, 'playername'), // extract the player names
      (v, k) => `playername${+k > 0 ? +k + 1 : ''}` // create the keys
    )
  }))
  .value()
  
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
Answer:3

You can use reduce and Map. Here Map is used to keep track of number of palyername is used for particular id

const arr = [{"id":1,"name":"Liquid","playername":"GH"},{"id":1,"name":"Liquid","playername":"KuroKy"},{"id":1,"name":"Liquid","playername":"Miracle"},{"id":1,"name":"Liquid","playername":"w33"},{"id":1,"name":"Liquid","playername":"Mind-Control"}]

let groupData = (arr) => {
  let mapper = new Map()
  return Object.values(arr.reduce((op,{id, name, playername }) => {
    mapper.set(id, ( mapper.get(id) || 0) + 1 )
    let key = mapper.get(id)
    op[id] = op[id] || { id, name }
    op[id][`playername${key}`] = playername
    return op
  },{}))
}

console.log(groupData(arr))
Answer:4

Just using using reduce to group the array into an object and Object.values to convert the object into an array.

let list = [
{"id": 1,"name": "Liquid","playername": "GH",},
{"id": 2,"name": "Solid","playername": "KuroKy",},
{"id": 1,"name": "Liquid","playername": "Miracle",},
{"id": 1,"name": "Liquid","playername": "w33",},
{"id": 2,"name": "Solid","playername": "Mind-Control",}
];

let counter = {};
let result = Object.values(list.reduce((c, v) => {
  if (!c[v.id]) {
    counter[v.id] = 0;
    c[v.id] = {...v};
  } else c[v.id]["playername" + ++counter[v.id]] = v.playername;
  return c;
}, {}));


console.log(result);
Answer:5

I have an fuctions in JS like below. manager.addDocument('en', 'bye bye take care', 'greetings.bye'); manager.addDocument('en', 'okay see you later', 'greetings.bye'); manager.addDocument('en', ...

I have an fuctions in JS like below. manager.addDocument('en', 'bye bye take care', 'greetings.bye'); manager.addDocument('en', 'okay see you later', 'greetings.bye'); manager.addDocument('en', ...

  1. make each sentence negative
  2. make each sentence true
  3. make each sentence
  4. make a sentence each with the words you wrote
  5. make each number sentence true
  6. how to make each sentence appear in powerpoint
  7. make a sentence with each other
  8. make a sentence using each
  9. make a sentence with each word
  10. lo contrario make each sentence negative
  11. write numbers to make each sentence true
  12. use parentheses to make each sentence true
  13. make a sentence using word each
  14. make a sentence from each of the following irregular verbs
  15. make a sentence from each of the following irregular verbs arise-arose
  16. make a sentence using each idiom above
  17. make a sentence from each of the following irreguler verbs
  18. make three questions to each sentence

I would like to extract numbers from a string such as There are 1,000 people in those 3 towns. and get an array like ["1,000", "3"]. I got the following number matching Regex from Justin in this ...

I would like to extract numbers from a string such as There are 1,000 people in those 3 towns. and get an array like ["1,000", "3"]. I got the following number matching Regex from Justin in this ...

  1. regex matching multiple characters
  2. regex matching multiple words
  3. regex matching multiple groups
  4. regex matching multiple lines
  5. regex matching multiple patterns
  6. regex matching multiple digits
  7. regex matching multiple times
  8. regex matching multiple strings
  9. regex match multiple
  10. regex match multiple words in any order
  11. regex match multiple spaces
  12. regex match multiple occurrences
  13. regex match multiple numbers
  14. regex match multiple lines python
  15. regex match multiple whitespace
  16. regex match multiple occurrences of group
  17. regex match multiple newlines
  18. regex match multiple file extensions
  19. regex match multiple words in string
  20. regex match multiple conditions

So I have an array of objects that I receive like this: [ { accountName: { type: 'link', value: { value: '1234567890123456789', to: '/q?Id=1237896540789654', },...

So I have an array of objects that I receive like this: [ { accountName: { type: 'link', value: { value: '1234567890123456789', to: '/q?Id=1237896540789654', },...

  1. recursive function that conversion decimal to octal
  2. recursive function that reverses a string
  3. recursive function that
  4. recursive function for factorial
  5. recursive function to find factorial
  6. recursive function for binary search
  7. recursive function for factorial in c
  8. recursive function for palindrome in python
  9. recursive function for factorial in python
  10. recursive function for gcd
  11. recursive function for palindrome
  12. recursive function for tower of hanoi
  13. recursive function for linear search
  14. recursive function to find gcd
  15. recursive function for sum of n numbers
  16. recursive function to calculate factorial
  17. recursive function to find factorial in c
  18. recursive function with example
  19. recursive function to reverse a number
  20. recursive function to find power of a number

I want to use React-Router to change the <Frontpage /> Component with the <Question /> Component after a click on "Los geht's". Please ignore the handleClick method, it was one of my ...

I want to use React-Router to change the <Frontpage /> Component with the <Question /> Component after a click on "Los geht's". Please ignore the handleClick method, it was one of my ...

  1. react router replace vs push
  2. react router replace history
  3. react router replace state
  4. react router replace url
  5. react router replace params
  6. react router replace query params
  7. react router replace route
  8. react router replace example
  9. react router replace hash
  10. react router replace path
  11. react router replace location
  12. react router replace not working
  13. react router replace search
  14. react router replace
  15. react router replace component
  16. react router replace link
  17. react router replace push
  18. react router replace query
  19. react router replace pathname
  20. react router replace function