Arrays inside objects [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create an object that inside him have some fields, one of them supposed to be an array.
var ob=
{
name : "asdad"
array ?
}
But I dont know how to.
Can anyone help me?
Thank you!

a value is just a value, whether it be a simple scalar or a complicated looking object.
var obj = {
name: "user name",
stocks: ["AAPL", "GOOG"],
cars: [ {make:"Honda", model: "accord"},
{make:"BMW", model: "525", year: 2014}
]
};

do something like this:
var ob= {
name : "asdad",
myArray : ["value1", "value2", "etc."]
}
It would be helpful for you to go over this MDN doc.

Related

Create JSON with wrapped name in javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For example i have JSON like this
{
"1":"12",
"2":"13",
"3":"14"
}
How can i wrap them like
"data":{
"1":"12",
"2":"13",
"3":"14"
}
Any lib we can do this ?
Thank you a lots
const json = `{
"1": "34.4",
"dog":"Snoopy",
"more": [ 1, 2, 3, 4 ]
}`
const myObj = {
data: JSON.parse(json)
}
console.log(myObj);
Simply that would do it. JSON is simply a string format, representing a javascript object. So within myObj, I can assign that to an object property and parse that JSON string into an object.

How to loop over a nested JavaScript object in jQuery? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to Javascript and jQuery. I am trying to create something like this (object) below and read the keys and retrieve the values.
var members = {unit:1,name: ["AA", "AB"],userid:["0001","0002"],
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]};
if 'Unit' == 1 then display name array.
Basically I want loop through the object and retrieve the value.
I think you member should be like this
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
members.forEach(function(v,i){
if(v.unit==1)
{
console.log(v.name);
}
});
I guess this is want you wanted, an object within an object. Where 'unit' is the key for retrieving the objects within the object.
var members = {
1: {
name: ["test","test"],
userid: ["0001","0002"]},
2: {
name: ["test2", "test2"],
userid: ["0011", "0012"]}
};
console.log(members[1]);

Convert array values into strings in javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this array [ABC, QWE, XYZ]
I would like to turn it into ['ABC', 'QWE', 'XYZ']
When I try to manipulate values in the current array I get: ReferenceError: ABC is not defined
Any ideas on how should I do it?
Thanks!
Convert arrays element types:
Number to strings
var strArr = [1,2,3,4,5].map(String);
// Result: ["1","2","3","4","5"]
We can't do that directly but after little bit change you can do that...
So the current array you said like array [ABC, QWE, XYZ],
Lets design you keys in object first:
var obj = {
ABC:1, QWE:'somevalue', XYZ:new Date()
}
So I created object obj having your variables lets say the three variables, now lets convert:
var arr = [];
for (var key in obj){
console.log(key, obj[key]);
arr.push(String(key));
}
console.log(arr);// you will see the desire result.
Running example here : example

Liferay - Set Expando Field Value in a Javascript Function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to set a User Expando field value in a Javascript Function in liferay. It is possible to do?
You can use the json api like this way
Liferay.Service(
'/expandovalue/add-value',
{
companyId: 20154,
className: 'com.liferay.portal.model.User',
tableName: 'CUSTOM_FIELDS',
columnName: 'test',
classPK: 30924,
data: 'test'
},
function(obj) {
console.log(obj);
}
);
where
columnName is the name of your custom field
classPK is the entity id in this case userId
data the value of the custom field

How can I add items to existing array? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array like this one:
var data1 = [{ value: 'Afghan afghani', lubID: 'AFN' },{ value: 'Albanian lek', lubID: 'ALL' }];
What i want to do is to dynamically add items to data1 after it has been created. How can i do that?
Thank you.
Just like you push to any other array:
data1.push( { value: 'Something', lubID: 'Something Else'} );
If you're starting from an empty array, just define it first:
var data1 = [];
then start adding values using the push method I've shown you above.

Categories