Replace string by an other variable with a string - javascript

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);

Related

Replace array elements based on dictionary in javascript

I have a dictionary like this :
var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'}
I have an array like this :
var isoCodes = ['AFG','AFG','AFG','AGO,'AGO','AFG','AFG','AND','AGO']
I want to replace the above array to give me an output like this:
var isoCodes - ['AF','AF','AF','AO,'AO','AF','AF','AD','AO']
Just use map
var iso_map = {'AFG':'AF','ALB':'AL','DZA':'DZ','ASM':'AS','AND':'AD','AGO':'AO','AIA':'AI','ATA':'AQ','ATG':'AG'};
var isoCodes = ['AFG','AFG','AFG','AGO','AGO','AFG','AFG','AND','AGO']
var result = isoCodes.map(x => iso_map[x]);
console.log(result);

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");

Convert string to become variable

I have multiple variables containing JSON as string (received from AJAX).
data.output_data_1234
data.output_data_5678
I convert them to Array:
var outputdataarr = new Array(data.output_data_1234);
This works fine, but how do I add a number to the var name:
var outputdataarr = new Array('data.output_data_'+formid+'');
this one does not work.
formid contains a proper number.
This does not work too:
var outputvar = window['data.output_data_' + formid];
var outputdataarr = new Array(outputvar);
Please help. Thanks.
You probably mean, you need something like this:
var outputdataarr = new Array(data['output_data_'+formid]);
You can only use string in square brackets as an object field identifier. It cannot contain '.'.
UPDATE:
However, you will probably need a loop to fill the whole array, e.g.
var outputdataarr = new Array();
for (var i=1000; i<2000; i++) {
outputdataarr.push(data['output_data_'+formid]);
}
Use [] instead of new Array is better.
var outputdataarr = [];
outputdataarr.push(data['output_data_'+formid]);
//and so on

Categories