-1

I am trying below code but it's not working as expected. I wanted to check if the cell Value has India or UnitedStatedofAmerica or Germany or Switzerland then go inside and if it's undefined, null or blank, or it doesn't contain the text location then go to else and set the value to ROW, But its not working.

So each time it give me results ROW even if cell have India or UnitedStatedofAmerica and when its null it goes inside the true condition and throw error for split as cell.Value is null

3 Example cell.Value

"Lorem Ipsum is simply dummy text of the printing and typesetting;Location India, Mumbai;Lorem Ipsum is simply dummy text of the printing and typesettingLorem Ipsum is simply dummy text of the printing and typesetting"

"Location India;Lorem Ipsum is simply dummy text of the printing and typesettingLorem Ipsum is simply dummy text of the printing and typesetting;Lorem Ipsum is simply dummy text of the printing and typesettingLorem Ipsum is simply dummy text of the printing and typesetting"

"Lorem Ipsum is simply dummy text of the printing and typesettingLorem Ipsum is simply dummy text of the printing and typesetting;Lorem Ipsum is simply dummy text of the printing and typesettingLorem Ipsum is;Location United Stated of America; simply dummy text of the printing and typesetting"

if (cell.Value.split('Location')[1].split(',')[0].replace(/\s/g, "") === "India" || cell.Value.split('Location')[1].split(',')[0].replace(/\s/g, "") === "Germany" || cell.Value.split('Location')[1].split(',')[0].replace(/\s/g, "") === "UnitedStatesofAmerica" || cell.Value.split('Location')[1].split(',')[0].replace(/\s/g, "") === "Switzerland" || cell.Value !== 'undefined' || cell.Value !== null || cell.Value !== "" || cell.Value.toLowerCase().indexOf("Location") !== -1) {
  persona.country = cell.Value.split('Location')[1].split(',')[0].replace(/\s/g, "");
} else {
  persona.country = "ROW"
}

5
  • 1
    Please provide a minimal reproducible example which demonstrates the problem. "It's not working" doesn't tell us anything, for all we know your data may simply not be what you assume it is. This is also a good time for you to do some debugging. There are a lot of operations being performed in that condition. You could extract them into their own variables and check the values of those variables to see if each of those operations does what you expect. Commented Apr 30, 2022 at 17:37
  • 1
    please add some examples of cell.Value. Commented Apr 30, 2022 at 17:52
  • @NinaScholz added few example Commented Apr 30, 2022 at 17:56
  • btw a to lower cased string can never contain an uppercase letter like 'Location'. what is this (later comparison) doing? Commented Apr 30, 2022 at 17:58
  • there are some cell.Value where i dont have Location field so for that as well it should show ROW Commented Apr 30, 2022 at 18:00

2 Answers 2

1

You could simplify some parts with some variables and an array of wanted countries.

const
    value = cell.Value || '',
    country = value.split('Location')?.[1]?.split(',')[0].replace(/\s/g, ""),
    countries = ["India", "Germany", "UnitedStatesofAmerica", "Switzerland"];

persona.country = countries.includes(country)
    ? country
    : "ROW";
Sign up to request clarification or add additional context in comments.

3 Comments

it do work when countries for array and for other countries but there are case where cell.Value is null or cell.Value does not have Location
what should happen if null (not a string) or not having location?
if its null or not having Location then as well it should set value ROW
0

If cell.Value is null or undefined then calling .split will throw an error. You could just add a falsy check for cell.Value at the beginning of the toif statement.

if(cell.Value && cell.Value.split...

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.