英文:
How do i iterate through a specific object in an array
问题
我有一个包含对象的数组,我需要获取对象"tipo"的值(而不是键)。
const productsArray = [
{
id: 1,
title: "Tacos",
price: 30,
score: 40,
description: "Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
tipo: {
campechano: 25,
costilla:25,
cspaldilla:25,
lomo:25,
maciza:25,
panza:25,
pescuezo:25,
surtida:25
}
},
{
id: 2,
title: "Kilogramos",
price: 500,
score: 89,
description: "Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
tipo: {
campechano: 500,
costilla:50,
cspaldilla:50,
lomo:50,
maciza:50,
panza:50,
pescuezo:50,
surtida:50
}
},
{
id: 3,
title: "Consome",
price: 50,
score: 25,
description: "Delicioso Consome de Borrego con arroz y garbanzo",
tipo: {
chicho: 30,
grande: 50
}
}
]
我尝试了以下代码:
for (let key in productsArray){
console.log(`${key}${productsArray[key]}`);
}
但是我得到了以下输出:
0[object 0bject]
1[object 0bject]
2[object 0bject]
而不是:
25
25
25
25
25
25
25
25
500
500
500
500
500
500
500
500
30
50
我无法弄清楚如何获得这些输出,有什么提示吗?
英文:
i have an array with objects and i need to get just the values(not the keys) of the object "tipo".
const productsArray = [
{
id: 1,
title: "Tacos",
price: 30,
score: 40,
description: "Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
tipo: {
campechano: 25,
costilla:25,
cspaldilla:25,
lomo:25,
maciza:25,
panza:25,
pescuezo:25,
surtida:25
}
},
{
id: 2,
title: "Kilogramos",
price: 500,
score: 89,
description: "Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
tipo: {
campechano: 500,
costilla:50,
cspaldilla:50,
lomo:50,
maciza:50,
panza:50,
pescuezo:50,
surtida:50
}
},
{
id: 3,
title: "Consome",
price: 50,
score: 25,
description: "Delicioso Consome de Borrego con arroz y garbanzo",
tipo: {
chicho: 30,
grande: 50
}
}
]
i tried these:
for (let key in productsArray){
console.log(`${key}${productsArray[key]}`);
}
but i get
0[object 0bject]
1[object 0bject]
2[object 0bject]
instead of:
25
25
25
25
25
25
25
25
500
500
500
500
500
500
500
500
30
50
i can't figure out how to get these outputs, some tips?
答案1
得分: 0
你想要使用flatMap()
和Object.values()
。
const productsArray = [
{
id: 1,
title: "Tacos",
price: 30,
score: 40,
description:
"Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
tipo: {
campechano: 25,
costilla: 25,
cspaldilla: 25,
lomo: 25,
maciza: 25,
panza: 25,
pescuezo: 25,
surtida: 25,
},
},
{
id: 2,
title: "Kilogramos",
price: 500,
score: 89,
description:
"Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
tipo: {
campechano: 500,
costilla: 50,
cspaldilla: 50,
lomo: 50,
maciza: 50,
panza: 50,
pescuezo: 50,
surtida: 50,
},
},
{
id: 3,
title: "Consome",
price: 50,
score: 25,
description: "Delicioso Consome de Borrego con arroz y garbanzo",
tipo: {
chicho: 30,
grande: 50,
},
},
];
const result = productsArray.flatMap((x) => Object.values(x.tipo));
console.log(result);
解释:
对于productsArray
中的每个项,你想要一个只包含tipo
对象的值的项,你可以使用Object.values()
来获取这些值。因此,你可以使用map()
来实现,但是由于你还想将所有的值放在一个列表中,所以你需要将其展平,这就是为什么你想要使用flatMap()
的原因。
英文:
You wanna use flatMap()
and Object.values()
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const productsArray = [
{
id: 1,
title: "Tacos",
price: 30,
score: 40,
description:
"Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
tipo: {
campechano: 25,
costilla: 25,
cspaldilla: 25,
lomo: 25,
maciza: 25,
panza: 25,
pescuezo: 25,
surtida: 25,
},
},
{
id: 2,
title: "Kilogramos",
price: 500,
score: 89,
description:
"Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
tipo: {
campechano: 500,
costilla: 50,
cspaldilla: 50,
lomo: 50,
maciza: 50,
panza: 50,
pescuezo: 50,
surtida: 50,
},
},
{
id: 3,
title: "Consome",
price: 50,
score: 25,
description: "Delicioso Consome de Borrego con arroz y garbanzo",
tipo: {
chicho: 30,
grande: 50,
},
},
];
const result = productsArray.flatMap((x) => Object.values(x.tipo));
console.log(result);
<!-- language: lang-css -->
/* Stackoverflow: show only console */
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
<!-- end snippet -->
Explanation
For each item in your productsArray
you want to have one item which is just the values of the tipo
objects, which you get by using Object.values()
. So to do that you would use map()
, but as you also want to have all the values in a single list you need to flatten that, that's why you wanna use flatMap()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论