访问 Object.keys adonis.js 中的特定 属性 (Node.js)
Access a specific property in Object.keys adonis.js (Node.js)
我有一个查询 returns 一个数组,带有 Object.key (array) .foreach 我正在迭代,我想知道特定数组中 属性 的值。
示例:
Object.keys(arreglo).forEach(function(key) {
console.log(arreglo[key]);
});
输出为:
姓名:"Pepito",
姓氏:"Perez"
我想知道如何只获取姓氏的值
我知道它不会起作用,但它会是这样的:
console.log(arreglo[key].surname);
这就是你要找的吗
const object1 = {
a: {firstname:"sali",lastname:"mali"},
b: {firstname:"sali",lastname:"mali"},
c: {firstname:"sali",lastname:"mali"}
};
Object.keys(object1).forEach(function(key){console.log(object1[key].lastname)});
您可以在原始数组上使用 Array.forEach
,如下所示。您甚至可以使用 Array.map
.
提取您感兴趣的字段
// let's assume the arrary you got from your query is in this format
const arreglo = [
{ firstname: "fn1", surname: "ln1"},
{ firstname: "fn2", surname: "ln2"},
{ firstname: "fn3", surname: "ln3"}
];
// you can log `surname` for each entry in the array
arreglo.forEach(v => console.log(v.surname));
// you can use map to transform the array to just have `surname` using array.map()
const surnames = arreglo.map(v => v.surname);
console.log(surnames);
我有一个查询 returns 一个数组,带有 Object.key (array) .foreach 我正在迭代,我想知道特定数组中 属性 的值。 示例:
Object.keys(arreglo).forEach(function(key) {
console.log(arreglo[key]);
});
输出为: 姓名:"Pepito", 姓氏:"Perez" 我想知道如何只获取姓氏的值 我知道它不会起作用,但它会是这样的:
console.log(arreglo[key].surname);
这就是你要找的吗
const object1 = {
a: {firstname:"sali",lastname:"mali"},
b: {firstname:"sali",lastname:"mali"},
c: {firstname:"sali",lastname:"mali"}
};
Object.keys(object1).forEach(function(key){console.log(object1[key].lastname)});
您可以在原始数组上使用 Array.forEach
,如下所示。您甚至可以使用 Array.map
.
// let's assume the arrary you got from your query is in this format
const arreglo = [
{ firstname: "fn1", surname: "ln1"},
{ firstname: "fn2", surname: "ln2"},
{ firstname: "fn3", surname: "ln3"}
];
// you can log `surname` for each entry in the array
arreglo.forEach(v => console.log(v.surname));
// you can use map to transform the array to just have `surname` using array.map()
const surnames = arreglo.map(v => v.surname);
console.log(surnames);