131

Is it possible to do create a list of your own objects in Javascript? This is the type of data I want to store :

Date : 12/1/2011   Reading : 3   ID : 20055    
Date : 13/1/2011   Reading : 5   ID : 20053    
Date : 14/1/2011   Reading : 6   ID : 45652
1
  • 3
    List does not works on javascript. It will show "List not defined" error in console. You can try Array, Set, Map etc for this purpose. Commented Oct 18, 2018 at 9:12

6 Answers 6

190
var list = [
    { date: '12/1/2011', reading: 3, id: 20055 },
    { date: '13/1/2011', reading: 5, id: 20053 },
    { date: '14/1/2011', reading: 6, id: 45652 }
];

and then access it:

alert(list[1].date);
Sign up to request clarification or add additional context in comments.

4 Comments

I would be adding each item to the list individually, what would be the correct syntax to do that?
i think it would be like that list[list.length]= {date : '11/11/1999',reading:3,id=1}; Regards
@user517406, like this: var list = []; list.push({ date: '12/1/2011', reading: 3, id: 20055 });.
@DarinDimitrov would accessing individual records be easier or more efficient by creating an object with unique id's as properties? The date and reading being properties of each id node? In other words var myReadings = {} then access values with myReadings.20055.reading? Build it out by looping your array, for example myReadings[list[0]["id"]] = list[0]["id"]; Or, is performance the same - array of objects vs object w properties?
74

dynamically build list of objects

var listOfObjects = [];
var a = ["car", "bike", "scooter"];
a.forEach(function(entry) {
    var singleObj = {};
    singleObj['type'] = 'vehicle';
    singleObj['value'] = entry;
    listOfObjects.push(singleObj);
});

here's a working example http://jsfiddle.net/b9f6Q/2/ see console for output

Comments

29

Maybe you can create an array like this:

var myList = new Array();
myList.push('Hello');
myList.push('bye');

for (var i = 0; i < myList .length; i ++ ){
    window.console.log(myList[i]);
}

Comments

8

Going off of tbradley22's answer, but using .map instead:

var a = ["car", "bike", "scooter"];
a.map(function(entry) {
    var singleObj = {};
    singleObj['type'] = 'vehicle';
    singleObj['value'] = entry;
    return singleObj;
});

1 Comment

Why use map? Asking because I really don't know, not to be "clever" ;-)
0

Instantiate the array

  list = new Array()

push non-undefined value to the list

    var text = list.forEach(function(currentValue, currentIndex, listObj) {
    if(currentValue.text !== undefined)
    {list.push(currentValue.text)}
    });

Comments

-6

So, I'm used to using

var nameOfList = new List("objectName", "objectName", "objectName")

This is how it works for me but might be different for you, I recommend to watch some Unity Tutorials on the Scripting API.

2 Comments

List does not works on javascript. It will show "List not defined" error in console.
Unity Scripting != Javascript.

Your Answer

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.