0

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(),
      };
4
  • 1
    Please show a Javascript object representation of what you're starting with and what you to end up with. Also, I don't understand what the original content is that could be "comma delimited", but not a string? What is that? Commented Jun 16, 2020 at 23:25
  • i have multiple values that look something like this : region : Gorzów Wlkp, woj. lubuskie i want the output to be something like this city : Gorzów Wlkp place : woj. lubuskie for all those values as for what am starting with am parsing json data that looks like this : "entries": [ { "name": "Lokalizacja", "content": "Gorzów Wlkp, woj. lubuskie", "icon": "icon-pin" } Commented Jun 16, 2020 at 23:31
  • Apparently, someone else guessed the right answer, but next time please put the desired input and output data structure IN your question. Commented Jun 17, 2020 at 0:22
  • yes i will , thank you for your help Commented Jun 17, 2020 at 0:23

1 Answer 1

1

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(),
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.