如何解析数组对象,其中每个键在数组中具有多个值?

时间:2019-01-23 15:48:57

标签: javascript parsing arraylist

我要迭代该响应,然后将该响应填充到我创建的数据表中。

我尝试使用键值对读取,但是找不到适合我的响应类型的任何相关方法。

{"1":["Node Down",1,10,19.18,null,null,null,"48.73"],
"2":["Multiple Alerts",2,8,1.37,null,null,null,"35.52"],
"3":["Blank call",3,12,17.81,null,null,null,"24.72"],
"4":["Account Locked or expired or creation",4,13,23.29,null,null,null,"24.46"],
"5":["High Space Used",5,15,32.88,null,null,null,"21.79"],
"6":["Password reset",6,16,30.14,null,null,null,"19.1"]}

我想迭代结果,然后将结果填充到我创建的dcreated.s中。此响应是动态的,并且对于我正在获取的一个帐户而言。在我的代码中,我只需要[0],[2],[3],[7]索引的值。任何想法我们如何实现?

1 个答案:

答案 0 :(得分:0)

使用.entries并过滤具有您要查找的键的值:

const data = {"1": ["Node Down", 1, 10, 19.18, null, null, null, "48.73"],"2": ["Multiple Alerts", 2, 8, 1.37, null, null, null, "35.52"],"3": ["Blank call", 3, 12, 17.81, null, null, null, "24.72"],"4": ["Account Locked or expired or creation", 4, 13, 23.29, null, null, null, "24.46"],"5": ["High Space Used", 5, 15, 32.88, null, null, null, "21.79"],"6": ["Password reset", 6, 16, 30.14, null, null, null, "19.1"]};

const listOfKeys = ['1', '3', '5', '6'];
console.log(Object.entries(data).filter(([key, val]) => listOfKeys.includes(key)).map(([key, val]) => ({ [key]: val})));