1

I have an service that some time return data structure like this

params:value;

and then sometime return

params:[value1, value2];

I want to change if value is String (first case), then delete it and make an empty params:[]. How can I aechive it?

4
  • What have you tried? Commented Jul 27, 2016 at 14:23
  • JSON is JavaScript object notation... treat it like Javascript in Javascript.... Commented Jul 27, 2016 at 14:24
  • o.params = typeof o.params === "string" ? [] : o.params; Commented Jul 27, 2016 at 14:27
  • @nicael I checked if it is a string by instanceof and then assign it as new array but nothing happen. Edit: Nvm, I wrote the wrong params:( Commented Jul 27, 2016 at 14:48

1 Answer 1

1

After you parse JSON you can use typeof operator to see if value is string and if it is change it to empty array [].

var data = {
  params: 'value'
}

if(typeof data.params === 'string') data.params = [];
console.log(data);

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.