Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript array in array

I am building a file management system for the web right now. But I have some problems with javascript array's.

In the system there is an opportunity to add labels to file's.
In javascript I want to have the ID and the value's of the labels with the fileId in 1 array.(as below). I also want the FileId and the LabelId not as the index of the array's. Because the FileId and labelId can be a realy high number. And then I have an array full of undefined items.

Here an example of how I would like to have it:

array[FileId][labelId,labelValue]

If you have an solution please help me.

Thanks.

like image 753
f_blom Avatar asked Nov 29 '25 06:11

f_blom


1 Answers

You can form structure like this:

arr = [{FieldId:fid_value, Labels:[{labelId:lid_value, labelValue:label_text}]}]

Basically, an array with objects. Each object contains two fields: field id and labels. Labels is an array with objects also. Each object has label id and label value property.

Code to create new items might be like this:

arr = array();
fieldObj = {FieldId:fid_value, Labels:[]};
fieldObj.Labels.push({labelId:lid_value, labelValue:label_text});
fieldObj.Labels.push({labelId:lid_value, labelValue:label_text});
fieldObj.Labels.push({labelId:lid_value, labelValue:label_text});
...
arr.push(fieldObj);
like image 117
Viktor S. Avatar answered Nov 30 '25 20:11

Viktor S.