Javascript/Array question.

Hi. I have more experience with PHP than javascript and I’m trying to wrestle javascript into doing what I want.

What I’m doing is reading a text file with each line formatted like this : “name@description”. Now I want to have the first part before the “@” as a “key” and the second part as the value, but I just can’t get it to work. What I want is an array that looks lite this:

myArray[“bob”] == “value1”
myArray[“frank”] == “value2”
myArray[“pete”] == “value3”
myArray[“abaddon”] == “value4”

… so that I can access the value for for examle “bob” by doing this : pickedName = myArray[“bob”] and get ‘pickedName = “value1”’.

I realize that this is a noob question, which makes it even more frustrating. How would I do this? Here’s my code so far:

var objectdescriptions : TextAsset;
internal var lines : String[];
internal var splitthoughts;
var CleanDescriptions = {};

function Start()
{
	lines = objectdescriptions.text.Split("\n"[0]);

	for (line in lines)
	{
		splitthoughts = line.Split("@"[0]);
		if (splitthoughts[0] != '')
		{
			CleanDescriptions[splitthoughts[1]] = splitthoughts[0];
			print(CleanDescriptions[splitthoughts[0]]);
		}
	}
}

Thanks!

I would use a dictionary instead of array:

#pragma strict

import System.Collections.Generic;

    var objectdescriptions : TextAsset;
    private var myDict: Dictionary.<String,String>;
     
    function Start()
    {
        var lines: String[] = objectdescriptions.text.Split("\n"[0]);
     	myDict = new Dictionary.<String,String>();
        for (var line:String in lines)
        {
            myDict[line.Split("@"[0])[0]] = line.Split("@"[0])[1];
        }
    }

Thanks a lot! That’s works perfectly. Another question while I’m here; how do I list all “keys” in a dictionary? I mean similar to “print_r” in PHP, which lists all keys and values in one go.

var myList = new List.<String>(myDict.Keys);
var myValues = new List.<String>(myDict.Values);