我有这个对象数组:
var hola = [{key:id , desc:description }];
// they are filled with data with ajax from a table
这是现在的值
var hola = [
{key: "AL-32021611", descc: "7500"},
{key: "AL-32021612", descc: "Continental"},
{key: "AL-32021612", descc: "R3"},
{key: "AL-32021612", descc: "7500"}
];
我需要的是,我得到一个给定的值,例如
newarr= {key:AL-32021612}[descc:"continental", "r3", "7500"]
但是从这个'newarr'我只需要像某种索引那样的关键字来获取var中的所有descc值而且我真的输了,这就是为什么
$.each(newarr, function (i, data)
{ trHTML = '<div class="three_col_to_one_col categories">' + idVal[i] +'</div>' })
我将使用这个newarr作为每个的长度并打印其内容,但我只需要这个descc值,它复杂和凌乱但我是新的,我可能得到downvotes但是感谢阅读我
答案 0 :(得分:0)
创建一个空对象以用作字典,其中键是ID&#c;键,值是&#34; descc&#34; s的数组。像这样填写:
var hola = [
{key: "AL-32021611", descc: "7500"},
{key: "AL-32021612", descc: "Continental"},
{key: "AL-32021612", descc: "R3"},
{key: "AL-32021612", descc: "7500"}
];
var dict = {};
hola.forEach(obj => {
if (!dict[obj.key]) dict[obj.key] = [];
dict[obj.key].push(obj.descc);
});
console.log(dict);
&#13;
然后ID可以访问每个descc
数组:dict["AL-32021612"]
将是数组["Continental", "R3", "7500"]