Max
Max

Reputation: 442

Spliting the values inside an object in javascript

I have some values in an object that are comma separated but stored in a field called "region" I want to be able to split the values on the comma and store the first one into a field called "city" and the second into a field call "place" I can only call the split method on string values so how could implement something like this on these kinds of values?

The code used :

 let entry = {
        ...list[i],
        phone_number: contact.phones.length > 0 ? contact.phones[0].number : "",
        region: entryData.summary.offer.delivery.summary[0].value.text || "",
        startingAt: entryData.summary.offer.publication.startingAt || "",
        endingAt: entryData.summary.offer.publication.endingAt,
        date: new Date().toLocaleDateString(),
      };

Upvotes: 0

Views: 38

Answers (1)

see sharper
see sharper

Reputation: 12025

You mean like this?

const loc = (entryData.summary.offer.delivery.summary[0].value.text || ",").split(',');
let entry = {
  ...list[i],
  phone_number: contact.phones.length > 0 ? contact.phones[0].number : "",
  city: loc[0],
  place: loc[1],
  startingAt: entryData.summary.offer.publication.startingAt || "",
  endingAt: entryData.summary.offer.publication.endingAt,
  date: new Date().toLocaleDateString(),
};

Upvotes: 1

Related Questions