如何在数组中迭代特定对象?

huangapple go评论92阅读模式
英文:

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: &quot;Tacos&quot;,
    price: 30,
    score: 40,
    description:
      &quot;Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz&quot;,
    tipo: {
      campechano: 25,
      costilla: 25,
      cspaldilla: 25,
      lomo: 25,
      maciza: 25,
      panza: 25,
      pescuezo: 25,
      surtida: 25,
    },
  },
  {
    id: 2,
    title: &quot;Kilogramos&quot;,
    price: 500,
    score: 89,
    description:
      &quot;Delicioso kilo de barbacoa hecha al horno con le&#241;a y sin hueso&quot;,
    tipo: {
      campechano: 500,
      costilla: 50,
      cspaldilla: 50,
      lomo: 50,
      maciza: 50,
      panza: 50,
      pescuezo: 50,
      surtida: 50,
    },
  },
  {
    id: 3,
    title: &quot;Consome&quot;,
    price: 50,
    score: 25,
    description: &quot;Delicioso Consome de Borrego con arroz y garbanzo&quot;,
    tipo: {
      chicho: 30,
      grande: 50,
    },
  },
];

const result = productsArray.flatMap((x) =&gt; 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().

huangapple
  • 本文由 发表于 2023年8月9日 02:12:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定