Push an object from an array into another array javascript

Viewed 106

I am having an issue with pushing an object into an array. I am fetching an array of objects from a local .json file. I am trying to create a function that pushes the given object into an array, so I can use it later on.

This is how I am receiving my object.

{id: 3176, name: "Matthias Ginter", position: "Defender", dateOfBirth: "1994-01-03T00:00:00Z", countryOfBirth: "Germany", …}
countryOfBirth: "Germany"
dateOfBirth: "1994-01-03T00:00:00Z"
id: 3176
name: "Matthias Ginter"
nationality: "Germany"
position: "Defender"
role: "PLAYER"
__proto__: Object

My code in React:

{data.map(
                        (player) => (
                            player.position == null ? (player.position = "Coach") : null,
                            (
                                <PlayerCard
                                    name={player.name}
                                    position={player.position}
                                    dateOfBirth={player.dateOfBirth}
                                    nationality={player.nationality}
                                    id={player.id}
                                    handleClick={() => selectedPlayers.push(player)}
                                />
                            )
                        )
                    )}

and the array I am trying to push into:

let selectedPlayers = [
        {
            id: 3176,
            name: "Matthias Ginter",
            position: "Defender",
            dateOfBirth: "1994-01-03T00:00:00Z",
            countryOfBirth: "Germany",
            nationality: "Germany",
            role: "PLAYER",
        },
        {
            id: 3176,
            name: "Mat Giensien",
            position: "Defender",
            dateOfBirth: "1994-01-03T00:00:00Z",
            countryOfBirth: "Germany",
            nationality: "Germany",
            role: "PLAYER",
        },
    ];

if I do handleClick = {() => {console.log(player)}} I get the object as shown above. What am I missing ?!

I read through all the topics here in SO but couldn't find a solution that would work for me. I have tried all the reccomended ways in the other topics and they still don't work for me.

Please, halp meeeeh :)

2 Answers
{data.map(player => {
    return (
        <PlayerCard
            name={player.name}
            position={player.position ? "Coach" : null}
            dateOfBirth={player.dateOfBirth}
            nationality={player.nationality}
            id={player.id}
            handleClick={() => selectedPlayers.push(player)}
        />
    )
)}

Try this:

let selectedPlayers = [
    {
        id: 3176,
        name: "Matthias Ginter",
        position: "Defender",
        dateOfBirth: "1994-01-03T00:00:00Z",
        countryOfBirth: "Germany",
        nationality: "Germany",
        role: "PLAYER"
    },
    {
        id: 3176,
        name: "Mat Giensien",
        position: "Defender",
        dateOfBirth: "1994-01-03T00:00:00Z",
        countryOfBirth: "Germany",
        nationality: "Germany",
        role: "PLAYER"
    }
];

let newData = {id: 9999, name: "bob jones", position: "goalkepper", dateOfBirth: "2020-01-03T00:00:00Z", nationality: "Spain", role: "MANAGER"}

 selectedPlayers.push(newData);


 console.log( selectedPlayers );

You might have to mess around with the formatting of the recevied object and the object you're pushing into to make iy work properly. But, if you're in control of the code and the JSON file then that shouldn't be an issue...

Related