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.

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);
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"
]

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

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.

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.