how can I variable values to store a array?
Look my code:
var collection =normal,user,student ;
var array = [collection];
alert(array[0]);
In this case, alert would popup a normal,user,student. but i need an array such as array[0] get normal,array[1] get user,array[2] get student like that
how it is possible
Is there any chance to convert such variable into a JS array?
As normal,user,student are values. You can use split() to split the string using , as deliminator then indexes can be used to access elements.
var collection = "normal,user,student";
var array = collection.split(',');
console.log(array[0]);
There are many, many ways to create arrays ... some examples:
// just declare an array directly
var array1 = ["normal", "user", "student"];
console.log(array1[0]);
// use split to create an array out of a string
var collection = "normal,user,student";
var array2 = collection.split(",");
console.log(array2[1]);
// use split and map to create an array by your needs
var collection = " normal, user , student ";
var array3 = collection.split(",").map(function(value) {
return value.trim();
});
console.log(array3[2]);
// push each value to the array
var array4 = [];
array4.push("normal");
array4.push("user");
array4.push("student");
console.log(array4[0]);
// ...
var collection = "normal,user,student";
var jsarray = collection.split(",");
alert(jsarray[0]);
alert(jsarray[1]);
alert(jsarray[2]);
Are you trying to add the existing variables to the array? If so you are just missing your square brackets:
var collection = [normal, user, student];
If you would like the elements to contain the string values, you would do it like this:
var collection = ["normal", "user", "student"];
Related
i have array like this
[["apple","banana"],["monkey"]];
how can i associate key to them like,
[{"fruit":["apple","banana"],"wild":["monkey"]}]
is this possible?
i'm trying something like this
var arr = [["apple","banana"],["monkey"]];
var newArray = [];
for(var i=0;i<arr.length;i++){
newArray["fruit"] = arr[i] //further code i don't know
}
help me th
While you can do that, like this:
var array = [["apple","banana"],["monkey"]];
var update = [
{fruit: array[0], wild: array[1]}
];
console.log(update);
...frankly it seems unlikely that's really want you want.
Very simply you can assign a new name simply
var array = [["apple","banana"],["monkey"]];
var names = ["fruits", "wild"]
var modified = [{names[0]: array[0], names[1]: array[1]}];
If you have a lot of values just use a for loop to iterate and assign value to it.
var json = [];
var id = "displayImage1";
var object = "style";
storearray(id, object);
function storearray(_id, _object){
json.push(_id);
_id.push(_object);
return
}
i am making a function to store array inside a array then return it, here is my code , it didt work can anyone point out what i did wrong ? or show me how to make function then store array inside array.
so the array will be like this json -> id -> object
Demo
It looks like you're not looking for an array, but for an object, with the id as keys and the object as a value.
Is this what you're looking for?
var json = {};
json[id] = object
This would result in json containing {'displayImage1': 'style'}
I'm trying to understand how to add a value into my array. It is multidimensional:
var eventcontent = {
'2015-05-02' : [{'title':'somethingtitle1','content':'somethingcontent1','something':'something1'},{'title':'somethingtitle2','content':'somethingcontent2','something':'something2'}],
'2015-05-07' : [{'title':'somethingtitle7','content':'somethingcontent7','something':'something7'}],
}
How can I achieve adding the following data into the '2015-05-02'?
{'title':'somethingtitle3','content':'somethingcontent3','something':'something3'}
thanks for your help
eventcontent is object. Firstly you have to gain access to array stored under 2015-05-02 key. 2015-05-02 is not valid property identifier so you can't access it via
var array = eventcontent.2015-05-02 // SyntaxError
instead you have to use bracket notation
var array = eventcontent['2015-05-02'];
then you can for example push your data to the array
var data = {'title':'somethingtitle3','content':'somethingcontent3','something':'something3'};
var array = eventcontent['2015-05-02'];
array.push(data);
Edit:
Probably you should also check if array actually exist so your code becomes:
var data = {'title':'somethingtitle3','content':'somethingcontent3','something':'something3'};
var array = eventcontent['2015-05-02'];
if (array === undefined) // check if it is undefined and if so...
array = eventcontent['2015-05-02'] = []; // make empty array and assign it to eventcontent under '2015-05-02' key
}
array.push(data);
I am using an array as defined below
cat_id = Object { 2="text", 3="TKL1", -1="Select an Attribute"}.
when i am trying to retrieve the length of "cat_id" as
var l = cat_id.length;
its showing the length as "undefined".
But, I am unable to get the length of "cat_id". I am unable to use replace(), indexOf() and split() functions.
for example:
var index = cat_attr.indexOf(",")
Its showing error as below:
TypeError: cat_attr.indexOf is not a function
This isn't array, this is Object. Array defined in this way: [1,2,3]. If you want retrieve the "length" of object you can do this in this way:
var students = {job:92, adam:67,sara:83};
var studentNames = Object.keys(students);
var studentsLength = studentNames.length;
If you want split object to array you can do this in this way:
var students = {jon:92, adam:67,sara:83};
var studentNames = Object.keys(students);
var studentArray = studentNames.map(function(name){
var student = {};
student.name = name;
student.grade = students[name];
return (student);
}); // [{name:jon,grade:92},{name:adam,grade:67},{name:sara,grade:83}]
cat_id variable you defined as not an array, it is a javascript object. That is why it is not behaving like array! [] is the array notation. Use array notation [] instead of {} object notation you have used.
Your problem is wrong type and wrong syntax.
In your question your cat_id is an Object, not string or array. So you can not call length() or indexOf() funtion.
Moreover, you typed wrong syntax, it should be:
var cat_id = { 2: 'text', 3: 'TKL1', -1: 'Select an Attribute'}.
Here is how to retrieve property count (size and/or length)
size_obj = Object.keys(my_object).length;
As in
size_cat_id = Object.keys(cat_id).length;
Assuming an object is initialized as following:
var myObj = {
"key1":"val1",
"key2":"val2",
"key3":"val3",
...
};
Can I retrieve key values like this?
var retrKey1 = myObj[0];
var retrKey2 = myObj[1];
var retrKey3 = myObj[2];
...
The issue I am trying to solve is that I need to pick random key values from this object. Generating a random number is not an issue, but:
How can I retrieve the number of keys in the object/map?
Can I retrieve the key values using a integer index like in arrays?
If not, what are my options?
The Object.keys method returns an array of object properties. You can index the array with numbers then.
var myObj = {
"key1":"val1",
"key2":"val2",
"key3":"val3",
...
};
var keys = Object.keys(myObj);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
No, because there's no ordering among property keys. If you want ordered keys, you need to work with an array.
You could define a structure like this :
var myObj = [
{key:"key1", val:"val1"},
...
];