JavaScript。从关联数组中提取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5721101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:07:57  来源:igfitidea点击:

JavaScript. Extract values from associative array

javascriptarrays

提问by Jonathan Clark

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

如何从 JavaScript 中的关联数组中获取值?

I just need the email addresses and not the labels.

我只需要电子邮件地址而不是标签。

(
  {
    office = ("[email protected]");
    home = ("[email protected]");
    work = ("[email protected]");
  },
  {
    home = ("[email protected]");
  }
)

UPDATE: Prefered output in JSON would be:

更新:JSON 中的首选输出是:

{
    "data": [
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        }
    ]
}

Thankful for all input!

感谢所有输入!

回答by pawlik

What you probably meant to do is:

你可能想要做的是:

var x = [{
 office: ("[email protected]"),
 home: ("[email protected]"),
 work: ("[email protected]")
},
{
 home: ("[email protected]")
}]

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 practiceto use for … in.

// 编辑:但是,这不是用于 ... in的最佳实践

Maybe you could change your data structure to:

也许您可以将数据结构更改为:

var x = [[{
        value: "[email protected]",
        type: "office"
    },
    {
        value: "[email protected]",
        type: "home"
    },
    {
        value: "[email protected]",
        type: "work"
    }],
    [{
        value: "[email protected]",
        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);
    }
}

回答by Tejs

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

回答by Sapphire_Brick

Here's a one-liner:

这是一个单行:

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

where var assoc = // your associative array.

哪里var assoc = // your associative array

回答by Carmine Ingaldi

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

是您输入的 JSON 格式吗?因为如果是这样,那就是错误的语法。然而

let _in = [
 {
   office : "[email protected]",
   home : "[email protected]",
   work : "[email protected]",
 },
 {
   home : "[email protected]"
 }
]

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

对于每条记录,我提取值并“打包”到一个带有“电子邮件”属性的对象中,然后我合并了从原始记录数组中获得的所有数组

回答by pawlik

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.