3

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.

asked Jun 18, 2013 at 17:24

5 Answers 5

1

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);
answered Jun 18, 2013 at 17:30
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not entirely sure what you're asking but array within array is possible...

a = []
a.push('a')
Result:
["a"]
a.push(['hello','world'])
Result:
["a", 
 Array[2]
 0: "hello"
 1: "world"
]
answered Jun 18, 2013 at 17:27

Comments

1

It sounds like you want objects instead of arrays:

var obj = {};
obj["fieldName"] = {label: "labelname", labelId: 1234};

Then you can access this data as:

obj["fieldName"].label
answered Jun 18, 2013 at 17:31

Comments

0

You could also use an object

var data = {};
data["item1"] = { "labelId" : "foo1", "labelValue" : "bar1" };
data["item2"] = { "labelId" : "foo2", "labelValue" : "bar2" };
console.log(data.item1.labelId);

There are plenty of ways you can strcture the object, it is normally better to use an object than to remember that index 0 is the id and that index 1 is a value.

answered Jun 18, 2013 at 17:30

Comments

0

Use should use objects as well as arrays:

var root = [{
 id: '12345',
 metadata: {
 label: 'foo',
 },
 type: 'folder',
 name: 'Folder Name',
 children: [...]
 }
];

Now, you can iterate through the folders and files in your root:

for (var i = 0; i < root.length; i++) {
 var item = root[i];
 console.log(item.type, item.name, item.id);
}
answered Jun 18, 2013 at 17:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.