0

I have 2 array of objects one for USA and another for Canada. The data goes in the following way

const data = [
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"USA"}}
]

Here I am expecting to divide the data into 2 like as

USA: 
.....
.....

CAN:
.....
.....

This is what I've tried

const allData  = data.filter(item => item.country)

Here I'm getting all the array data but I want to sort it based on country name but hard coding isn't expected. Any optimal solution

5 Answers 5

2

you can do something like this

const data = [
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"USA"}}
]

const order = ['USA', 'CAN']

const reorder = data.sort((a, b) => {
  return order.indexOf(a.country.cntryShortName) - order.indexOf(b.country.cntryShortName)

})

console.log(reorder)

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

Comments

0

I think you would most likely have to have a new object where you will push the data in after looping over the array. Something like:

let allData = {};

data.forEach(function (countryData) {
    if (allData[countryData.country.cntryShortName] !== undefined) {
        // Push the data into the array
        allData[countryData.country.cntryShortName].push(countryData);
    } else {
        // Initialize an empty array if the country key doesn't exist
        allData[countryData.country.cntryShortName] = [];
    }
});

You'll end up with something like this:

{
    "USA": [
        {
            "country": {
                "cntryShortName": "USA"
            }
        },
    ],
    "CAN": [
        {
            "country": {
                "cntryShortName": "CAN"
            }
        },
    ]
}

So now you will just be able to grab the country data using:

allData.USA
// or
allData.CAN

Comments

0

JavaScript (Node.js), 74 bytes

f=(a,g=e=>parseInt(e.country.cntryShortName,36))=>a.sort((a,b)=>g(a)-g(b))

Try it online!

Comments

0

You can use this function :

let groupByCountry = arr => {
    return arr.reduce((rv, x) => {
      (rv[x['country']['cntryShortName']] = rv[x['country']['cntryShortName']] || []).push(x);
      return rv;
    }, {});
  };

let sortedData = groupByCountry(data);

Comments

0

you can do something like this :


const USA_DATA = data.filter(item => item.country.cntryShortName === "USA");

const CAN_DATA  = data.filter(item => item.country.cntryShortName === "CAN");

if you dont want to do hardcode then you can make it dynamic like this

let arr = data.map((it) => it.country.cntryShortName)
arr = [...new Set(arr)];
let newArr = arr.map((it) => {return data.filter(item => item.country.cntryShortName === it)})
console.log(newArr)

3 Comments

I have done this.... but, hard coding is not allowed
please check my other solution
ok I will check this solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.