很多时候我们需要对数组进行查找值,一般是循环对比比较多,这里有个办法可以轻松解决该问题。
var a = [
{
"cmd": "text",
"desc": "this is one"
},
{
"cmd": "image",
"desc": "this is two"
},
{
"cmd": "button",
"desc": "this is thire"
}
]
var b = {}
a.map(function(command, i){
b[command.cmd] = command;
})
console.log(b["text"]); // { cmd: 'text', icon: 'icon-1' }
console.log(b["text"].desc); // this is one