pixelcreated
pixelcreated

Reputation: 196

Pushing split JSON back into JSON object

I have a JSON file which can have multiple FromName values. I need to split these into separate values, and push back into the JSON data under FromName (retained the first name) and FromExtra (for the extra name).

I have managed to create the split successfully, however I am unable to get the split to push back into the JSON data in the right place under the right name.

Here is my JSON file:

[
    {
        "Name": "News 1",
        "FromName": "John Citizen, Jane Doe",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 2",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 3",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    }
]

Here is the code I have put together so far:

 for (var i = 0; i < abcPre.length; i++) {
    if (abcPre.FromName = []) {
        var sepNames = (abcPre[i].FromName).split(',');
        console.log(sepNames);          

        if (sepNames <= [1]) {
        var FromExtra = sepNames [1];
        console.log(`The new first name ${FromName}`);
        console.log(`The second from name ${FromExtra}`);
        abcPre[i].push.FromExtra;
        console.log(abcPre);
        }


        
    }       
    
    else {
        // console.log(`SentDate loop not working`);
    }
}

Closest I have been able to get is to push the information back into the JSON file, but the push is ending up in the wrong place (see below).

[
    {
        "Name": "News 1",
        "FromName": "John Citizen, Jane Doe",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 2",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 3",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    },
    {
        "John Citizen"
    }
]

This is the output which is what I am trying to achieve:

[
    {
        "Name": "News 1",
        "FromName": "John Citizen",
        "FromExtra": "Jane Doe",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 2",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    },
    {
        "Name": "News 3",
        "FromName": "John Citizen",
        "FromEmail": "[email protected]"
    }
]

Thank you in advance. Javascript is something I am still learning with.

Upvotes: 0

Views: 111

Answers (3)

pixelcreated
pixelcreated

Reputation: 196

for(let i = 0; i < arr.length; i++){
    let splitted = arr[i].FromName.split(', ')
    if(splitted.length > 1){
        arr[i].FromName = splitted[0];
        arr[i].ExtraName = splitted[1]
    }
}

console.log(arr)

Upvotes: 0

Ajmal Noushad
Ajmal Noushad

Reputation: 946

I'm assuming, it will have a maximum of two names in FromNames field from your example.

Try this:

const result = abcPre.map((item) => {
  [FromName, FromExtra] = item.FromName.split(', ');
  return FromExtra ? {
     ...item,
     FromName,
     FromExtra
  } : item;
})

console.log(result)

Upvotes: 0

sonEtLumiere
sonEtLumiere

Reputation: 4562

You can split() by a comma and then creates the ExtraName property, try this:

let arr = [
{
    "Name": "News 1",
    "FromName": "John Citizen, Jane Doe",
    "FromEmail": "[email protected]"
},
{
    "Name": "News 2",
    "FromName": "John Citizen",
    "FromEmail": "[email protected]"
},
{
    "Name": "News 3",
    "FromName": "John Citizen",
    "FromEmail": "[email protected]"
}
]

for(let i = 0; i < arr.length; i++){
    let splitted = arr[i].FromName.split(', ')
    if(splitted.length > 1){
        arr[i].FromName = splitted[0];
        arr[i].ExtraName = splitted[1]
    }
}

console.log(arr)

Upvotes: 1

Related Questions