从JSON对象创建数组

Del*_*Sam -1 javascript arrays json object

下面是我的JSON对象

var validation_messages ={
"test1": [{
    "1": {

        "rssi": -25,

    }
}, {
    "2": {

        "rssi": -25,

    }
}],
"test2": [{
    "15": {

        "rssi": -10,
                }
}, {
    "19": {

        "rssi": -21,

    }
}]
Run Code Online (Sandbox Code Playgroud)

}

以下就是我的尝试

var rssival = []; for(validation_messages中的var key){

var obj = validation_messages[key];

for (var i=0; i<obj.length; i++) {
  for(x in obj[i]){

   rssival.push(obj[i][x].rssi);
  } 
}
Run Code Online (Sandbox Code Playgroud)

} console.log(rssival)

我得到输出为

[-25, -25, -10, -21]
Run Code Online (Sandbox Code Playgroud)

但我需要一个输出

[[-25, -25], [-10, -21]]
Run Code Online (Sandbox Code Playgroud)

谁能帮帮我吗?

Raj*_*esh 5

你可以尝试这样的事情:

var validation_messages={"00:11:74:41:3d:f0":[{1:{channel:1,type:2,rssi:-25,rate:24}},{2:{channel:1,type:2,rssi:-25,rate:24}}],"00:23:68:bd:6c:19":[{15:{channel:1,type:0,rssi:-10,rate:1}},{19:{channel:1,type:2,rssi:-21,rate:54}}]};

var result = Object.keys(validation_messages).map(function(k){
  var tmp = [];
  validation_messages[k].forEach(function(o){ 
    for(var k in o){
      tmp.push(o[k].rssi)
    }
  })
  return tmp
});
console.log(result)
Run Code Online (Sandbox Code Playgroud)