我有一个查询,返回一个带有Object.key(数组)的数组。foreach我正在迭代,我想知道特定数组中属性的值。 示例:
Object.keys(arreglo).forEach(function(key) {
console.log(arreglo[key]);
});
输出为: 名称:“ Pepito”, 姓:“ Perez” 我想知道如何仅获取姓氏的值 我知道这行不通,但是会像这样:
console.log(arreglo[key].surname);
答案 0 :(得分:0)
这就是您要寻找的
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)});
答案 1 :(得分:0)
您可以在原始数组上使用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);
提取您感兴趣的字段。
list.slice(0, 5).map((i) => {
return <div>{i}</div>
});