Creating an Array JS - javascript

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

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

Remove double quotes from an array in 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)

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

Javascript JSON stringify No Numeric Index to include in Data

i am trying to pass non numeric index values through JSON but am not getting the data.
var ConditionArray = new Array();
ConditionArray[0] = "1";
ConditionArray[1] = "2";
ConditionArray[2] = "3";
ConditionArray['module'] = "Test";
ConditionArray['table'] = "tab_test";
var Data = JSON.stringify(ConditionArray);
When i alert the Data Variable it has the Values 1,2 and 3 but module and table are not included. How can this be added so that the whole string is passed.
EDIT : And what if i have some multidimensional elements also included like
ConditionArray[0] = new Array();
ConditionArray[0] = "11";
JSON structure only recognizes numeric properties of an Array. Anything else is ignored.
You need an Object structure if you want to mix them.
var ConditionArray = new Object();
This would be an better approach:
var values = {
array : ["1", "2", "3"],
module : "Test",
table : "tab_test"
};
var data = JSON.stringify(values);
Since javascript array accepts numeric index only. If you want non numeric index,use Object instead.
var ConditionArray = {};
ConditionArray[0] = "1";
ConditionArray[1] = "2";
ConditionArray[2] = "3";
ConditionArray['module'] = "Test";
ConditionArray['table'] = "tab_test";
var Data = JSON.stringify(ConditionArray);
Here is the working DEMO : http://jsfiddle.net/cUhha/
According to the algorithm for JSON.stringfy (step 4b), only the (numeric) indices of arrays are stringified.
This is because Array does not contain your elements.
When you do this:
ConditionArray['module'] = "Test";
You actually add a property to the ConditionArray, not elements. While JSON.stringify converts to string only elements of the ConditionArray. For example:
var arr = new Array;
arr['str'] = 'string';
console.log(arr.length) //outputs 0
You need to use an Object instead of Array
If you change the first line to
var ConditionArray = new Object();
you will achieve the desired outcome.
If for some reason you cannot convert your array into object, for instance you are working on a big framework or legacy code that you dont want to touch and your job is only to add som feature which requires JSON API use, you should consider using JSON.stringify(json,function(k,v){}) version of the API.
In the function you can now decide what to do with value of key is of a specific type.
this is the way how I solved this problem
Where tblItemsTypeform is array and arrange is de index of the array
:
let itemsData = [];
for(var i = 0; i <= this.tblItemsTypeform.length -1;i++){
let itemsForms = {
arrange: i,
values: this.tblItemsTypeform[i]
}
itemsData.push(itemsForms)
}
And finally use this in a variable to send to api:
var data = JSON.stringify(itemsData)

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

Categories