Raj
Raj

Reputation: 81

i need to remove 1st single quote in this array using jquery

I created my function:

function call() {
    var value= [];
    var arr = data.split(',');
    for (i = 0; i < arr.length; i++) {
     var a = "'" + arr[i] + "'";
        value.push(a);
        alert(value);
    }
}

output is coming like this:

value='a','b','c','d';

but i need like this:

value=a','b','c','d;

helps are mostly appreciable. thank you

Upvotes: 4

Views: 611

Answers (4)

Adil
Adil

Reputation: 148110

You can use ternary operator ?: to put condition to add the starting quotes only after first element.

function call() {
    var value= [];
    var arr = data.split(',');
    for (i = 0; i < arr.length; i++) {
     var a = (i!=0 ? "'" : "") + arr[i] + "'";
        value.push(a);
        alert(value);
    }
}

You can also update the first element and remove start quote after loop complete.

function call() {
    var value= [];
    var arr = data.split(',');
    for (i = 0; i < arr.length; i++) {
     var a = "'" + arr[i] + "'";
        value.push(a);
        alert(value);
    }
    value[0] = value[0].substring(0,value[0].length);
    value[value.length-1] = value[value.length-1].substring(1, value[value.length-1].length-1);
}

Upvotes: 4

Kathir
Kathir

Reputation: 1240

use ternary operator ?: at both places of adding single quote.

function call() {
  var value= [];
  var arr = data.split(',');
  for (i = 0; i < arr.length; i++) {
    var a = (i!=0 ? "'" : "") + arr[i] + (i!=arr.length-1 ? "'" : "");
    value.push(a);
    alert(value);
 }
}

Upvotes: 0

xdazz
xdazz

Reputation: 160833

I don't know why you want to do such thing, but you could achieve this just by simply join with ','

var arr = data.split(',');
alert(arr.join("','"))

Simple code simple life :)

Upvotes: 4

Scary Wombat
Scary Wombat

Reputation: 44834

How about a simple regex replace

data.replace ("'[A-z]");

Upvotes: 0

Related Questions