简体   繁体   English

JavaScript。 从关联数组中提取值

[英]JavaScript. Extract values from associative array

How can I get the values from this associative array in JavaScript?如何从 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:更新:JSON 中的首选输出是:

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

Thankful for all input!感谢所有输入!

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. // 编辑:但是, 这不是用于 ... 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);
    }
}

Here's a one-liner:这是一个单行:

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

where assoc is your associative array.其中assoc是您的关联数组。

Edit编辑

I have a better answer here .在这里有一个更好的答案。

You can 'foreach' over the object to get it's properties:您可以在对象上“foreach”以获取其属性:

for(var j = 0; j < mySet.length; j++)
{
    for(var propName in mySet[j])
    {
        var emailAddress = mySet[j][propName];
        // Do Stuff
    }
}

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 .结果保存在ret

Is it your input in JSON format?是您输入的 JSON 格式吗? 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对于每条记录,我提取值并“打包”到一个带有“电子邮件”属性的对象中,然后我合并了从原始记录数组中获得的所有数组

看来您正在寻找Object.values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM