How to loop over a nested JavaScript object in jQuery? [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 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]);

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.

Adding array into an certain array index [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 want to populate a nested array.
The first array option_array is created like this
var option_array =[];
if(!option_array.includes(row.option.option_id))
{
option_array .push({
option_id:row.option.option_id,
option_name:row.option.option_name,
max:row.option.max,
min:row.option.min,
value:row.option.value,
price:row.option.price
})
}
And now, I am creating another array option_group
var option_group=[];
if(!option_group.includes(row.option_group_id))
{
option_group.push({
option_group_id:row.option_group_id,
option_group_name:row.option_group_name,
max:row.max,
min:row.min,
option:option_array
})
}
And I want to modify option_group whereas it will add only option:option_array where current row.option_group_id is equal to the option_group.option_group.option_group_id
.includes checks if the array includes a certain item. Since you have an array of objects, you can't check if the array includes row.option_group_id. You can use find instead. Get the object with row.option_group_id from the array. If it exists, update it. Else, add a new object to the array
const found = option_group.find(a => a.option_group_id == row.option_group_id)
if (found) {
found.option = row.option_array; // update the found object
} else {
option_group.push({
option_group_id: row.option_group_id,
option_group_name: row.option_group_name,
....
})
}

Not able to Parse a JSON [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 am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))

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

Arrays inside objects [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 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.

Categories