1

I have a model returned by a library in the following format:

var givenData = [{"fName": "john"}, {"fName": "mike"}, {"country": "USA"}]

How do I group the "fName" together and add '[]' to get:

{ 'fName[]': ['john','mike'],'country[]': ['USA'] };

**Note country and fName are not related at all.

7
  • you... loop over the array, and on each iteration, modify the output object based on the current array item... Commented Feb 6, 2017 at 21:52
  • Hmm.. why is this getting downvoted Commented Feb 6, 2017 at 21:58
  • probably because it's a work request rather than a problem. Commented Feb 6, 2017 at 21:58
  • 3
    It's getting downvoted because SO is not a code-writing service; please review How to ask a good question Commented Feb 6, 2017 at 21:59
  • Just curious, why the answers are downvoted then ? Commented Feb 6, 2017 at 22:00

4 Answers 4

1

Suggestion (using ES6 syntax)

const transformData = (data) => {
  const newData = {}
  data.forEach( (item) => {
    for (let key in item) {
       const newKey = key + "[]"
       if (!newData.hasOwnProperty(newKey)) newData[newKey] = []
       newData[newKey].push(item[key])
    }
  })
  return newData
}

/* added some extra keys just to test */
let givenData = [
  {"fName": "john", "country": "England"}, 
  {"fName": "mike"}, 
  {"country": "USA"},
  {"language": "English"}
]

console.log(transformData(givenData))
/*
{
    "fName[]": ["john","mike"],
    "country[]": ["England","USA"],
    "language[]":["English"]
}
*/
Sign up to request clarification or add additional context in comments.

Comments

1

You can iterate over the array and push the date to the desired field.

var givenData = [{"fName": "john"}, {"fName": "mike"}, {"country": "USA"}]

var result = {
  'fName[]': [],
  'country[]': []
};

givenData.forEach(function (data) {
  if (data.fName) {
    result['fName[]'].push(data.fName);
  }
  
  if (data.country) {
    result['country[]'].push(data.country);
  }
});

console.log(result);

Comments

1

You could take the key and build an object with the key and arrays as properties.

var givenData = [{"fName": "john"}, {"fName": "mike"}, {"country": "USA"}],
    grouped = givenData.reduce(function (r, o) {
        var key = Object.keys(o)[0] + '[]';
        r[key] = r[key] || [];
        r[key].push(o[Object.keys(o)[0]]);
        return r;
    }, Object.create(null));

console.log(grouped);

2 Comments

Whoever downvoted this answer, should leave a comment for the reason
Agreed, these are all good answers, someone downvoted all of them
0

In ES6:

const newData = givenData.reduce(function (r, o) { const key = `${Object.keys(o)[0]}[]`; return { ...r, [key]: [ ...r[key], o[key] ] } }, {});

This doesn't modify your original data and is very clean.

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.