-2

I have tried many times to group the location but doesn't work. hope help. thanks.

original json:

[
{_id: "1", description: "a", location: "us"}
{_id: "2", description: "b", location: "us"}
{_id: "3", description: "c", location: "tw"}
]

new json:

[
{data: [{_id: "1", description: "a"}, {_id: "2", description: "b"}], location: 'us'},
{data: [{_id: "3", description: "c"}], location: 'tw'}
]
kenny
  • 221
  • 2
  • 4
  • 13
  • 2
    Can you please add what you have tried till now... You can get answers here but let us know what you tried... – Code Spark Sep 02 '17 at 07:41
  • 2
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (If that *were* meant to be JSON, it would be invalid; in JSON, property names must be in quotes.) – T.J. Crowder Sep 02 '17 at 07:42
  • `I have tried many times` - how many? what methods? – Jaromanda X Sep 02 '17 at 08:06

3 Answers3

0

you can do it using

let arr = [
{_id: "1", description: "a", location: "us"},
{_id: "2", description: "b", location: "us"},
{_id: "3", description: "c", location: "tw"}
];
let result = [], map = {}, idx = 0;
for(let element of arr){
    let curr = 0;
    if(map[element.location] !== undefined){
        curr = map[element.location];
    }
    else{
        curr = idx;
        map[element.location] = idx++;
        result.push({data : [], location : element.location});
    }
    result[curr].data.push({_id: element._id, description: element.description});

}
console.log(result);
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

You could take the advanate of a hash table and group by the location.

var data = [{ _id: "1", description: "a", location: "us" }, { _id: "2", description: "b", location: "us" }, { _id: "3", description: "c", location: "tw" }],
    locations = Object.create(null),
    result = [];

data.forEach(function (o) {
    if (!locations[o.location]) {
        locations[o.location] = { data: [], location: o.location };
        result.push(locations[o.location]);
    }
    locations[o.location].data.push({ _id: o._id, description: o.description });
});

console.log(result); 
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Functional approach:

let arr = [
  {_id: '1', description: 'a', location: 'us'},
  {_id: '2', description: 'b', location: 'us'},
  {_id: '3', description: 'c', location: 'tw'}
]

let result = Object.entries(arr.reduce((h, {_id, description, location: l}) =>
  Object.assign(h, {[l]: [...(h[l] || []), {_id, description}]})
, {})).map(([l, d]) => ({location: l, data: d}))

console.log(result)

Or more readable approach:

let arr = [
  {_id: '1', description: 'a', location: 'us'},
  {_id: '2', description: 'b', location: 'us'},
  {_id: '3', description: 'c', location: 'tw'}
]

let dict = arr.reduce((dict, {_id, description, location}) => {
  dict[location] = dict[location] || []
  dict[location].push({_id, description})
  return dict
}, {})

let result = Object.entries(dict).map(([l, d]) => ({location: l, data: d}))

console.log(result)
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • seems the position is wrong when I console log it. for example I can see array(15) in chrome when I open it actually contain 3 object – kenny Sep 02 '17 at 15:56