0

There is a condition where i need to convert Array of objects into Array of Arrays. An array is given below as data convert this array as formatedArray.

data = [
      {
        "total": 1,
        "third": 0,
        "city": "Agra",
        "first": 0,
        "second": 0
      },
      {
        "total": 2,
        "third": 0,
        "city": "Agra",
        "first": 0,
        "second": 0
      },
      {
        "total": 3,
        "third": 0,
        "city": "Delhi",
        "first": 0,
        "second": 0
      },
      {
        "total": 4,
        "third": 0,
        "city": "Delhi",
        "first": 0,
        "second": 0
      }
]

How can I convert the array of objects above into an array of arrays below?

formatedArray=[
  "Agra":[
    {
      "total": 1,
      "third": 0,
      "city": "Agra",
      "first": 0,
      "second": 0
    },
    {
      "total": 2,
      "third": 0,
      "city": "Agra",
      "first": 0,
      "second": 0
    }
  ],
  "Delhi":[
    {
      "total": 4,
      "third": 0,
      "city": "Delhi",
      "first": 0,
      "second": 0
    },
    {
      "total": 4,
      "third": 0,
      "city": "Delhi",
      "first": 0,
      "second": 0
    }
  ]
]
3
  • just aggregate elems of your array by city: take a map, check if map has the city, if yes use the value to push the element, otherwise adds an empty city to the map and push it your elem Commented Oct 15, 2019 at 11:19
  • This is not an Array, it looks like a Json (Java). You don't state the language Commented Oct 15, 2019 at 11:36
  • What language are you using? ( Edit your post and include the corresponding tag next to arrays. ) Commented Oct 15, 2019 at 11:39

2 Answers 2

2

You can do it through vanilla JavaScript. The method which can be used is reduce() method:

const data = [
    {
      "total": 1,
      "third": 0,
      "city": "Agra",
      "first": 0,
      "second": 0
    },
    {
      "total": 2,
      "third": 0,
      "city": "Agra",
      "first": 0,
      "second": 0
    },
    {
      "total": 3,
      "third": 0,
      "city": "Delhi",
      "first": 0,
      "second": 0
    },
    {
      "total": 4,
      "third": 0,
      "city": "Delhi",
      "first": 0,
      "second": 0
    }
];

and the code looks like this:

const desired = data.reduce((acc, currentValue) => {
    acc[currentValue.city] = acc[currentValue.city] || [];        
    acc[currentValue.city].push(currentValue);    
    return acc;
}, {})

console.log(desired);
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the if: (acc[currentValue.city] exists from previous line) @StepUp
@user753642 you are right! It is great catch! Thanks for so great comment! :)
1

I suggesty you taking a look at the lodash groupby function:

https://lodash.com/docs/4.17.15#groupBy

formatedArray = _.groupBy(data, obj => obj.city);

Here is a quick stackblitz-example based on angular

https://stackblitz.com/edit/angular-dpj8jh

1 Comment

perfect! Glad it solved your problem. Would you mind accepting my answer as correct? Greetings

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.