3

How can I get the values from this associative array in JavaScript?

I just need the email addresses and not the labels.

(
  {
    office = ("my@email.com");
    home = ("ahome@anotheremail.com");
    work = ("nothing@email.com");
  },
  {
    home = ("test@test.se");
  }
)

UPDATE: Prefered output in JSON would be:

{
    "data": [
        {
            "email": "my@email.com"
        },
        {
            "email": "ahome@anotheremail.com"
        },
        {
            "email": "nothing@email.com"
        },
        {
            "email": "test@test.se"
        }
    ]
}

Thankful for all input!

simhumileco
  • 31,877
  • 16
  • 137
  • 115
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

6 Answers6

4

What you probably meant to do is:

var x = [{
 office: ("my@email.com"),
 home: ("ahome@anotheremail.com"),
 work: ("nothing@email.com")
},
{
 home: ("test@test.se")
}]

and:

for(var j = 0; j < x.length; j++)
{
    for(var anItem in x[j])
    {
        console.log(x[j][anItem])
    }
}

// EDIT: however, it's not the best practice to use for … in.

Maybe you could change your data structure to:

var x = [[{
        value: "my@email.com",
        type: "office"
    },
    {
        value: "ahome@anotheremail.com",
        type: "home"
    },
    {
        value: "nothing@email.com",
        type: "work"
    }],
    [{
        value: "test@test.se",
        type: "home"
    }]];

and iterate over using:

for( var i = 0, xlength = x.length; i < xlength; i++ )
{
    for( var j=0, ylength = x[i].length; j < ylength; j++ )
    {
        console.log(x[i][j].value);
    }
}
Community
  • 1
  • 1
pawlik
  • 2,385
  • 19
  • 18
  • I cannot change the data structure. I am developing an iPhone application in Appcelerator and this is what I get when I request information from the build in addressbook. They best would be if I could get the final output (addresses only) in JSON format. Is this possible? – Jonathan Clark Apr 19 '11 at 19:14
  • The current out put is now: [["my@email.com"],["ahome@anotheremail.com"],["nothing@email.com"],["test@test.se"]] in JSON. I would be cool if this could be: {"data": [{"email": "my@email.com"},{"email": "ahome@anotheremail.com"},{"email": "nothing@email.com"},{"email": "test@test.se"}]}. Is this possible? – Jonathan Clark Apr 19 '11 at 19:18
4

Here's a one-liner:

console.log(Object.keys(assoc).map(k => assoc[k]));

where assoc is your associative array.

Edit

I have a better answer here.

Sapphire_Brick
  • 1,560
  • 12
  • 26
1

You can 'foreach' over the object to get it's properties:

for(var j = 0; j < mySet.length; j++)
{
    for(var propName in mySet[j])
    {
        var emailAddress = mySet[j][propName];
        // Do Stuff
    }
}
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • I got an error with the above code (myObj) changed it to mySet and made an alert of emailAddress but each alert was empty. What is wrong? – Jonathan Clark Apr 19 '11 at 18:54
  • This time it worked perfectly! Thanks! Is it possible to rename all labels to the same name for example the word email instead of work? – Jonathan Clark Apr 19 '11 at 19:06
  • You can translate those values into another object of your choosing. – Tejs Apr 19 '11 at 19:43
  • he current out put is now: [["my@email.com"],["ahome@anotheremail.com"],["nothing@email.com"],["test@test.s­e"]] in JSON. I would be cool if this could be: {"data": [{"email": "my@email.com"},{"email": "ahome@anotheremail.com"},{"email": "nothing@email.com"},{"email": "test@test.se"}]}. Is this possible? – Jonathan Clark Apr 19 '11 at 19:44
0

Answer for edited question:

var ret = {data: []};

for(var j = 0; j < x.length; j++)
{
    for(var anItem in x[j])
    {
        ret.data.push({
            email: x[j][anItem]
        });
    }
}

console.log(ret);

The result is kept in ret.

pawlik
  • 2,385
  • 19
  • 18
0

Is it your input in JSON format? Because if so, it's the wrong syntax. However

let _in = [
 {
   office : "my@email.com",
   home : "ahome@anotheremail.com",
   work : "nothing@email.com",
 },
 {
   home : "test@test.se"
 }
]

let _out = []
_in.forEach( record => {  
   _out =  _out.concat(Object.values(record).map(x => new Object({email : x})))
})

console.log(_out)

for each record I extracted values and "packed" into an object with the "email" attrbiute, then I merged all those arrays obtained from the original array of records

Carmine Ingaldi
  • 856
  • 9
  • 23
0

It seems that you're looking for Object.values.

Sapphire_Brick
  • 1,560
  • 12
  • 26