4

i need to compare object key in array an need to group based on key value

const days = [
    {'name':'Mon','value':1}, 
    {'name':'Tue','value':5},
    {'name':'Wed','value':10},
    {'name':'Wed','value':30},
    {'name':'Fri','value':18},
    {'name':'Sat','value':80},
    {'name':'Sat','value':90},
    {'name':'Sun','value':20},
]

i need to group like this

const result [[1],[5], [10, 30], [18], [80, 90], [20]]

I am very new to javascript I not having any idea to do this. if its possible in lodash are es6 it will be fine.

4
  • 3
    Post your try to resolve problem code! Commented Jan 22, 2018 at 12:20
  • 1
    What is the logic behind grouping? Commented Jan 22, 2018 at 12:21
  • 1
    @HarshPatel Same day of a week? :) Commented Jan 22, 2018 at 12:22
  • you can achieve that with one reduce call which looks like this: days.reduce(fn, [[], [], [], [], [], [], []]) - try to figure out how fn should look like Commented Jan 22, 2018 at 12:22

5 Answers 5

3

You could just loop over the object, and create a map of key to data and then just get the values using Object.values(newObj)

const days = [
    {'name':'Mon','value':1}, 
    {'name':'Tue','value':5},
    {'name':'Wed','value':10},
    {'name':'Wed','value':30},
    {'name':'Fri','value':18},
    {'name':'Sat','value':80},
    {'name':'Sat','value':90},
    {'name':'Sun','value':20},
]

let res = {}
days.forEach(obj => {
    res = {...res, [obj.name]: [...(res[obj.name] || []), obj.value]}
})

console.log(Object.values(res));

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

3 Comments

thank for u reply i getting error on this line res = {...res, [obj.key]: [...(res[obj.key] || []), obj.value]}
instead [obj.name] i am using [obj.key] can u tell anything wrong on it
obj.key must exist for each of them and also it should be same for objects which have same name
2

You could take an object for same day and filter the result for getting only filled arrays.

var days = [{ name: 'Mon', value: 1 }, { name: 'Tue', value: 5 }, { name: 'Wed', value: 10 }, { name: 'Wed', value: 30 }, { name: 'Fri', value: 18 }, { name: 'Sat', value: 80 }, { name: 'Sat', value: 90 }, { name: 'Sun', value: 20 }], 
    slot = { Mon: 0, Tue: 1, Wed: 2, Thu: 3, Fri: 4, Sat: 5, Sun: 6 },
    result = [];

days.forEach(({ name, value }) => (result[slot[name]] = result[slot[name]] || []).push(value));

result = result.filter(Boolean);

console.log(result);

1 Comment

The only correct answer so far. Since it doesn't rely on object keys ordering.
0

With Lodash you can use groupBy and then map method to get desired result.

const days = [{"name":"Mon","value":1},{"name":"Tue","value":5},{"name":"Wed","value":10},{"name":"Wed","value":30},{"name":"Fri","value":18},{"name":"Sat","value":80},{"name":"Sat","value":90},{"name":"Sun","value":20}]

const result = _.chain(days)
  .groupBy('name')
  .values()
  .map(a => a.map(({value}) => value))
  
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Comments

0

Use Array#reduce to add the names and values to a map. Then get the Map's values iterator, and spread it back to an array:

const days = [{"name":"Mon","value":1},{"name":"Tue","value":5},{"name":"Wed","value":10},{"name":"Wed","value":30},{"name":"Fri","value":18},{"name":"Sat","value":80},{"name":"Sat","value":90},{"name":"Sun","value":20}]

const result = [...days.reduce((m, { name, value }) => {
  m.has(name) || m.set(name, []) // if the name is not in the map, add a new key with an empty array

  m.get(name).push(value) // get the array for the name, and push the value 

  return m 
}, new Map).values()] // convert the Map back to an array

console.log(result)

Comments

-1

You could use _.GroupBy , here is how you could do it.

 
const days = [
    { 'name': 'Mon', 'value': 1 },
    { 'name': 'Tue', 'value': 5 },
    { 'name': 'Wed', 'value': 10 },
    { 'name': 'Wed', 'value': 30 },
    { 'name': 'Fri', 'value': 18 },
    { 'name': 'Sat', 'value': 80 },
    { 'name': 'Sat', 'value': 90 },
    { 'name': 'Sun', 'value': 20 },
];

var t = _.groupBy(days, 'name');
result = [];
var s = Object.keys(t).forEach((i) => {
    var value = t[i];
    var temp = [];
    value.forEach((v) => {
        temp.push(v.value);
    });
    result.push(temp);
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

1 Comment

The resulting object isn't anything remotely like what the OP asked for.

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.