如果我有一个数组,我想提取每一行中的每个值(( : )之后的值)
Array =
0:Object {node-input-audio_services1: "79388", audio_ip: "127.0.0.1", audio_port_1: "7654"}
1:Object {node-input-video_services1: "80788", video_ip: "127.0.0.1", video_port_1: "7654"}
2:Object {node-input-audio_services1: "4fe10"}
3:Object {node-input-video_services1: "4fe10"}
想象一下我有几个key:value,然后我想像下面这样提取Just value:
Array =
0:Object {key1 : "value" , Key2 : "value2" , key3: "value3"}
1:Object {key1 : "value5" , Key2 : "value6" , key3: "value7"}
我得到这样的结果:
var result = ["value","value2",value3","value5","value6","value7"];
问题是,我无法使用(key1.array),因为程序中每次都有一个键的名称。我真的是JavaScript新手,不知道如何启动
答案 0 :(得分:0)
您可以映射数组中的每个对象,然后使用Object.values()方法提取每个值,如下所示:
const result = Array.map(e => Object.values(e));
答案 1 :(得分:0)
您可以将Array.reduce
与Object.values
结合使用,以从对象数组中获取 all 的值:
const arr = [{
key1: "value",
Key2: "value2",
key3: "value3"
},
{
key1: "value5",
Key2: "value6",
key3: "value7"
}
];
let result = arr.reduce(
(c, v) => c.concat(Object.values(v)), []
);
console.log(result);
答案 2 :(得分:0)
您可以尝试遍历数组并像这样连接值
results = results.concat( Object.values( yourArray[loopIndex] ) )
答案 3 :(得分:0)
const arr = [{
key1: "value",
Key2: "value2",
key3: "value3"
},
{
key1: "value5",
Key2: "value6",
key3: "value7"
}
];
let out = arr.reduce(
(c, v) => c.concat(Object.values(v)), []
);
console.log(out);
答案 4 :(得分:0)
var arr = [{ndeInput_audio_services1: "79388", audio_ip: "127.0.0.1", audio_port_1: "7654"},
{nodeInput_video_services1: "80788", video_ip: "127.0.0.1", video_port_1: "7654"},
{nodeInput_audio_services1: "4fe10"},
{nodeInput_video_services1: "4fe10"}];
如果我们有一个像您显示的对象数组,我将使用下划线库和可应用于数组的.values()函数。这是一个示例(下划线由_符号表示):
var result = [];
_.map(arr, obj => _.each(_.values(obj), val => result.push(val)));
首先,我们创建一个空数组来存储结果。接下来,我们映射数组中的每个对象。然后,我们返回与数组中每个对象关联的VALUE。然后,我们将该值推送到结果数组。结果将是与数组中每个键关联的所有值:
console.log(result);
[
'79388', '127.0.0.1',
'7654', '80788',
'127.0.0.1', '7654',
'4fe10', '4fe10'
]