Remove double quotes from an array in JavaScript - javascript

var old_response = ["a,b,c","x,y,z","e,f,g"]
new_response = ["a,b,c,x,y,z,e,f,g"]
Currently I am getting the old_response instead of that I need new_response

Simply join the elements using Array#join method and put into a new array.
var old_response = ["a,b,c","x,y,z","e,f,g"] ;
var new_response = [old_response.join()];
console.log(new_response);

old_response = ["a,b,c","x,y,z","e,f,g"]
console.log([old_response.join()])

You could cast your array to string:
var old_response = ["a,b,c","x,y,z","e,f,g"]
var new_response = [old_response.toString()] // or [old_response + '']
console.log(new_response)

Related

Get all match values from two comma separated string

I have two comma separated string as follows,
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
I need to create another string based on the above two,
if hiddenString have unmatching value with strB,then without those unmatched values need to create e new string and also avoid duplicates.
simply says, I need to get all the matching values from both strings.
As the example based on my two string, I'm expecting the following:
varFinalHiddenString = 14172,10062,100;
How can I do this using JavaScript and that should work in safari and IE 11 or its earlier versions. Please help me, I'm new to the JS.
You can first split() strings to generate arrays from them. Then filter() the smaller array by checking the index of the current item with indexOf() in other array:
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var temp1 = hiddenString.split(',');
var temp2 = strB.split(',');
var varFinalHiddenString = temp2.filter(function(s){
return temp1.indexOf(s) > -1;
}).join(',');
console.log(varFinalHiddenString);
Make arrays of the strings, then use the "filter" method. Then convert back to string.
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var hiddenStringAsArray = hiddenString.split(',');
var strBArray = strB.split(',');
var resultObject = $(strBArray).filter(hiddenStringAsArray);
var resultArray = resultObject.toArray();
var resultString = resultArray.join(',');
console.log(resultString);

String from jquery array element

I'm looking at arrays in jquery and have this issue, I need to assign a key with a town name, but struggling to understand how to deal with the spaces.
var hashtable = {};
hashtable['Bognor Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
var str = hashtable.Bognor Regis[0];
alert(str);
I thought perhaps I could do this
hashtable['Bognor-Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
var str = hashtable.Bognor-Regis[0];
then remove the - later, but it only seems to work if i have something like this
hashtable['BognorRegis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
What's the correct way of doing this ?
Thanks
If the keys have spaces you have to use the array accessor to retrieve them:
var hashtable = {};
hashtable['Bognor Regis'] = ["lat=50.782998&lng=-0.673061","Sussex"];
var str = hashtable['Bognor Regis'][0];
alert(str);
Example fiddle

Creating an Array JS

I create a variable from a title attribute tile="Red, Blue". Is there a simple way to make this an array. So the array has 2 values "Red" & "Blue"?
You can create an array as :
var title = "Red, Blue";
var array = tile.split(',');
OR ,
var array1 = document.getElementById('yourElementID')
.getAttribute('tile').split(',');
Do you mean splitting string?
var title = "Red, Blue";
var titleArray = title.split(", ");
This is what you are looking for:
var myArray = new Array("Red","Blue");

Replace string by an other variable with a string

I have this function:
var chart = d3.parsets()
.dimensions(["Survived", "Age", "Class"]);
I want to replace ["Survived", "Age", "Class"] with a string from an other variable. So i can change it. In this example: .dimensions(["Survived", "Age"]);
What i have is:
var strDimensions = ('"Survived", "Age"');
var chart = d3.parsets()
.dimensions([strDimensions]);
but that doesn't work
var strDimensions = ('"Survived", "Age"');
var chart = d3.parsets()
.dimensions([strDimensions]);
What you are getting is an array with one index, not two.
If you want to use the string, you can split it to make it into an array.
var strDimensions = ('Survived,Age');
var chart = d3.parsets()
.dimensions(strDimensions.split(",");
Why don't you just start off with the array to begin with?
Your array syntax is wrong, try this instead:
var strDimensions = ['Survived', 'Age'];
var chart = d3.parsets().dimensions(strDimensions);
what you need is
var dims = [];
now you can add and remove from the array as you wish
and then use
var chart = d3.parsets().dimensions(dims);

Storing value in a dynamically defined Javascript array?

I'm writing a script that will split the element's ID from the format 'note-1192' (for example) and place it into an array:
var timers = new Array();
$('.notes').keyup(function(){
var timerVariable = $(this).attr('id').split("-");
timerVariable = timerVariable[0];
timerVariable = timerVariable.replace('note', '');
alert(timerVariable); // 1192
timers[timerVariable] = timerVariable;
alert(timers.join('\n')); // blank
});
It's not storing the variable into the array at all, when I alert the 'timers array' it's empty, but when I alert the 'timerVariable' it comes out just fine.
Any suggestions would be very welcome, thanks!
The problem is the index syntax your are using is setting a named property on the array object not adding an element. To add an element to the array use push
timers.push(timerVariable);
try
Array:
var timers = new Array();
timers.push(timerVariable);
Hash:
var timers = {};
timers[timerVariable] = timerVariable;
Use push method of array to add an element to array.
timers.push(timerVariable);
var timers = new Array();
$('.notes').keyup(function(){
timers.push($(this).attr('id').split("-")[0]);
});
Despite the assertion in the comment,
"note-1192".split("-")[0].replace("note", "") === ""
The simple fix would be to get the second, rather than first, element of the split string:
var timerVariable = $(this).attr('id').split("-")[1];
To ensure you get a number, you could use a regular expression.
var timerId = $(this).attr('id').match(/\d+/);
if (timerId) {
timerId=timerId[0];
...
}
I think this is what you are trying to do:
var timers = new Array();
$('.notes').keyup(function(){
var temp = $(this).attr('id').split("-");
timerValue = temp[0];
timerID = temp[1];
alert(timerID); // 1192
timers[timerID] = timerValue;
alert(timers.join('\n'));
});
With the way you currently have it, timerVariable will be '' in the end, and so what you're doing at the end is really the same as:
timers[''] = '';

Categories