Deadpool
Deadpool

Reputation: 8240

Array Map function not able to push Object in simple Example

I got data where in an array I got set of objects (with country and states keys along with values).

Now, I need to format data in such a manner, that I got a new array (with countryName as key, and list of states -in array as values).

I am trying to do that but unsuccessful. Can someone help me out?

var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];

newData = []; 
data.map(item => newData.push({item.country: item.states});
console.log(newData);

Upvotes: 4

Views: 28114

Answers (6)

Al-Amin Piash
Al-Amin Piash

Reputation: 1

please use for loop, for async await condition, map not working well.

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Correct use of map is to return value in map method, which is missing in your code. Apart from that use destructure to further simplify.

const newData = data.map(({country, states}) => ({[country]: states}));

var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];

const newData = data.map(({country, states}) => ({[country]: states}));
console.log(newData);

Upvotes: 2

arslan
arslan

Reputation: 1144

let arr = []
array.forEach(element => {
  arr.push({key:element.country, value: element.state})
}

**if you use map then please return but i think you use forEach in such case

Upvotes: 0

Rajat Verma
Rajat Verma

Reputation: 307

You can try this:
It works well with your requirements.

data.forEach(item => {
    const obj = new Object();
    obj[item.country] = item.states;
    newData.push(obj)
});

Here we are using the new keyword to declare our Object and just pushing the object into our Array. Also, since country is a variable so we are using the [] notation rather than the dot notation.

Upvotes: 1

palaѕн
palaѕн

Reputation: 73916

That because object key with a dot is not a valid syntax. You can resolve this by wrapping the key with []. Also, .map() method creates a new array populated with the results of calling a provided function on every element in the calling array. So, you should either use .forEach() method here like:

data.forEach(item => newData.push({[item.country]: item.states})); 

var data=[{country:"United States",states:["Alabama","Alaska","Arizona"]},{country:"Canada",states:["Ontario","Saskatchewan","Alberta"]},{country:"Australia",states:["Australian Capital Territory","Jervis Bay Territory","New South Wales","Queensland"]}];

var newData = []; 
data.forEach(item => newData.push({[item.country]: item.states}));
console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Or, you can use .map() method and simply return the object inside the .map() callback and store that in newData like:

 newData = data.map(item => ({[item.country]: item.states}));

var data=[{country:"United States",states:["Alabama","Alaska","Arizona"]},{country:"Canada",states:["Ontario","Saskatchewan","Alberta"]},{country:"Australia",states:["Australian Capital Territory","Jervis Bay Territory","New South Wales","Queensland"]}]

var newData = data.map(item => ({[item.country]: item.states}));
console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 8

Sayooj V R
Sayooj V R

Reputation: 2363

Try like this.

var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];


let newData = data.map(item => { return { [item.country] : [...item.states]}})
console.log(newData);

Upvotes: 0

Related Questions